You always provide the best, easiest, and simplest approaches. I watch your explanations and then try to write the code independently, and I succeed most of the time. Keep making such great content. It really helps us a lot.
Very nicely explained. Your pace and explanation is super. I'm learning/revising DSA almost after 10 years and I should acknowledge that your videos are helping me a lot. Thank you very much for your efforts 🙏🙏
I was stucking this question for 4 days by watching many of videos but I can't find out the best way to answer this question. But you did and explain it really well. I know I'm not that good as problem solving but you are the one who can explain the concept from really hard to a really human can understanding really easy
@@officialdreamplayz just only 6-7. We are going slow because I think we are missing something or some concept to answer or design to answer the question. I think keep doing it will help us a lot. For me answering in Code war was really easy more than leetcode
bro i stuck at this problem and i dont understand how to solve this question but after watching some video i not getting it better but u solve this question with very simple approach. 👍👍 well done bro
great teaching bhaiya. I always look for ur videos whenever I search for a solution of a leetcode question. No one comes close to you when it comes to explaining multiple approaches and finally solving with the most optimal approach . Thanks bhaiya.
I really try to go over the concept, rather than just giving away the code...the same you would be solving a problem in an interview. Thanks for your support. Much appreciated. 😄
Interesting solution with sorting - but then you get n*log n. You can simply search the list for the shortest word and then compare it with all other words, thus making it O(n * k) where k is the length of the shortest word. Depending on the input you have in a real life scenario, your approach might be more efficient if the strings are very long, so if the length of the shoertest string k > n where n is number of values in the list, then your approach is more efficient. Anyway cool explanation thanks!
If the string contain a mixture of capital and small letter then sorting can't be the solution... string commonPrefix(vector& strs,int n){ string s1 = strs[0]; int ans_length = s1.length(); for(int i = 0 ; i< n ; i++){ int j = 0 ; while(j
Hey, found a similar problem but no one had a good solution online. The idea is to find the longest palindromic prefix. Would help a lot if you can make a video on this. thanks for the videos. love them.
The efficient solution isn't in fact the most optimal, sorting strings is not nlog(n) but the worst-case & average time is n^2log(n). Only in the best case where the first characters of the string do not match, the TC becomes O(long(n)).
Great explanation, but I would add a check for potential issue where an Index error can occur if the first and last strings in the strs array have different lengths. Like this: min_length = min(len(first), len(last)) for i in range(min_length)
That optimization only works in python or other interpreted languages, where sorting is much cheaper than a loop. In golang or c or c++ it's much faster to do loops, so you don't need to spend O(n log n) on sorting
#python:- x = ["flower", "flosing", "flowing"] prefix = "" for i in range(len(x[0])): flag = 0 temp = x[0][i] for j in range(1, len(x)): if not x[j][i] == temp: flag = 0 break else: flag = 1 if flag ==1: prefix+=temp else: pass print(prefix)
#this code for #python. def pre(a): a.sort() first=a[0] last=a[-1] out="" for i in range(len(first)): if first[i] == last[i]: out+=first[i] else: break return out print(pre(["flower","flow","flight"]))
if possible try to show the code for brute force approach also, this helps in understandig the problem in a much better way,btw doing great work,appreciated.
thanks for your feedback and support :)...but usually coding for brute force is not desirable...i only do that in scenarios when you tweak a brute force approach to convert it to an optimal solution.
But I think we can also do without sorting bcs ur comparing first and last word of the list and also how is the space complexity o(1) when ur appending Plus sorting itself will take o(n) Plus iteration of first word will take o(length of the word)
don't you think that for loop should also have the condition "&& i < last.lenght()" as if the first array has length greater, then it would also check that index char for the last array, which would result in "IndexArrayOutOfBonds" exception like the example: canada, car, czech (sorted) here the first element would have the length 6, so the for loop would also run for the value i = 5, but the last[5] doesn't exist
ok so I came up with the answer, its because the traversal of the string would just stop before it hits index more than the last element, correct me if i'm wrong
what is the time complexity of brute force. And Sorting must be also compare all the characters internally then just wondering how it is optimized solution
But i thought Array.sort() will take time complexity of O( n k log(n) ) where n is size of arrays and k is the length of longest string in arrays to sort lexicographical order ... So may it would be too expensive to use it. Now i could store our data in tries format instead of array format. but if we have still the array format then we should use bruteforce approche couse it will take only O(n * k)
i have a query , why we are comparing rhe first and the last string in the efficient solution after sorting the strings .. after sorting even first two strings can give the solution
All the 15 mins were worth it. Great job!
Thanks for the support and motivation
You always provide the best, easiest, and simplest approaches. I watch your explanations and then try to write the code independently, and I succeed most of the time. Keep making such great content. It really helps us a lot.
that is so nice to hear. I hope to keep bringing quality content for you guys.
Very nicely explained. Your pace and explanation is super. I'm learning/revising DSA almost after 10 years and I should acknowledge that your videos are helping me a lot.
Thank you very much for your efforts 🙏🙏
Definitely one of the easiest and best solutions that I came across. Thanks for the video!
When i saw the optimized solution, i really amazed. Thanks for helping in learning journey. 15mins completely worth it.
this breakdown made things much more clear and easy to comprehend
Esko dekh ye pta chala ki aise bhi question solve kr sakte hai.... keep posting
😄
Very Articulately explained . with proper animations etc , really good job
I was stucking this question for 4 days by watching many of videos but I can't find out the best way to answer this question. But you did and explain it really well. I know I'm not that good as problem solving but you are the one who can explain the concept from really hard to a really human can understanding really easy
how many questions u have conquered till now i m on 4th (easy)
@@officialdreamplayz just only 6-7. We are going slow because I think we are missing something or some concept to answer or design to answer the question. I think keep doing it will help us a lot. For me answering in Code war was really easy more than leetcode
all the very best...and glad to see your progress
bro i stuck at this problem and i dont understand how to solve this question but after watching some video i not getting it better but u solve this question with very simple approach.
👍👍 well done bro
So far one of the best solution I have seen
Your approach is really nice i didnt even think to sort array
great teaching bhaiya. I always look for ur videos whenever I search for a solution of a leetcode question.
No one comes close to you when it comes to explaining multiple approaches and finally solving with the most optimal approach .
Thanks bhaiya.
It's my pleasure
My bro, you are a great teacher!
tried a lot of videos and i would say this was the best explanation of all such a easy and elaborative explanation. thank you
Glad you feel this way ♥️
addicted to your channel. You explain so simple and in easy way. Thanks a lot
your videos seem like bit longer, but its really very productive, I couldnot even skip it for a sec, just great work!! Thank you!
I really try to go over the concept, rather than just giving away the code...the same you would be solving a problem in an interview. Thanks for your support. Much appreciated. 😄
Great style of teaching algorithms. 10/10 👍
excellent this channel will grow gradually ..no bakwas straight forward explanation ..
Thank you so much brother!!! Your explanation is extremely simple to understand and follow.
Wish I had a teacher like you.
loved the way you explained sir.....This channel is highly underrated
Thanks for your support and motivation. I really hope the youtube algorithm picks up the channel soon 😅
Interesting solution with sorting - but then you get n*log n. You can simply search the list for the shortest word and then compare it with all other words, thus making it O(n * k) where k is the length of the shortest word. Depending on the input you have in a real life scenario, your approach might be more efficient if the strings are very long, so if the length of the shoertest string k > n where n is number of values in the list, then your approach is more efficient. Anyway cool explanation thanks!
string longestCommonPrefix(vector& strs) {
int mini=INT_MAX;
int ind=-1;
for(int i=0;istrs[i].size()){
mini=strs[i].size();
ind=i;
}
}
string ans=strs[ind];
int j=mini-1;
while(j>=0){
int k=0;
//bool isOk=true;
//cout
If the string contain a mixture of capital and small letter then sorting can't be the solution...
string commonPrefix(vector& strs,int n){
string s1 = strs[0];
int ans_length = s1.length();
for(int i = 0 ; i< n ; i++){
int j = 0 ;
while(j
Thank you! The BEST on TH-cam. Super explanation.Keep up the great work.
Oh My GOD!!! I can't imagine that someone can be this good in explanation. Amazing and super thanks.
Wow, thanks!
Hey, found a similar problem but no one had a good solution online. The idea is to find the longest palindromic prefix. Would help a lot if you can make a video on this. thanks for the videos. love them.
Wow so you are finally tired of destroying world
man, what amazing explanation, thank you so much!
The efficient solution isn't in fact the most optimal, sorting strings is not nlog(n) but the worst-case & average time is n^2log(n). Only in the best case where the first characters of the string do not match, the TC becomes O(long(n)).
you are correct...happy to talk about an even more optimal solution if you have to bring up 🙂
Great explanation, but I would add a check for potential issue where an Index error can occur if the first and last strings in the strs array have different lengths.
Like this:
min_length = min(len(first), len(last))
for i in range(min_length)
Sure, you can do that
Love your Solution.
Best Wishes for You.
I Would Love to see your Channel Grow, why this is so Underrated.
Thanks for your support and motivation. I really hope the youtube algorithm picks up the channel soon 😅
I understand this problem with your explanation Easily❤
The level of Explanation I saw today was amazing !!!
So happy you felt that way
Very good explanation brother. This solution was so precise and easy to understand. Thanks a ton
glad you liked it.
OMG, you took teaching algorithm to next level
So happy you feel this way 😄
Awesome explaination. I liked the slow and animated explainartion. Good job!
Glad you liked it
You're such a good teacher. Thank you!
Glad you think so!
That optimization only works in python or other interpreted languages, where sorting is much cheaper than a loop.
In golang or c or c++ it's much faster to do loops, so you don't need to spend O(n log n) on sorting
Crystal clear concept thank you sir!
string longestCommonPrefix(vector& strs) {
sort(strs.begin(), strs.end());
string st=strs[0], end=strs[strs.size()-1];
string ans;
for(int i=0;i
bro can you explain why we go with first and last and explain why can't
we go with first and second string
Very good explanation ❤😊
Very Crisp and interesting explanation
this video made to subscriber to never loss hope and don't give up u will grow
Explained a complex problem very easily 💯👏
Excellent explanation 🔥 keep making more videos loved it
Thank you for great explanation. IMHO more efficiently is not to use StringBulider but just return first.substring(0, index)
The difference will be very minute. :)
Best Teacher. Please upload more videos. Super thanks
Great Explanation, small correction: Space complexity is O(n)
we aren't using any extra space to arrive at the solution
@@nikoo28 but we are using string builder it has the property to store n elements so the overall space complexity will be O(n)
keep them coming
Thanks for the explanation. Was easy to follow.
I love the way of explanation.Thank u so much making such a video bhaiya .
#python:-
x = ["flower", "flosing", "flowing"]
prefix = ""
for i in range(len(x[0])):
flag = 0
temp = x[0][i]
for j in range(1, len(x)):
if not x[j][i] == temp:
flag = 0
break
else:
flag = 1
if flag ==1:
prefix+=temp
else:
pass
print(prefix)
#this code for #python.
def pre(a):
a.sort()
first=a[0]
last=a[-1]
out=""
for i in range(len(first)):
if first[i] == last[i]:
out+=first[i]
else:
break
return out
print(pre(["flower","flow","flight"]))
if possible try to show the code for brute force approach also, this helps in understandig the problem in a much better way,btw doing great work,appreciated.
thanks for your feedback and support :)...but usually coding for brute force is not desirable...i only do that in scenarios when you tweak a brute force approach to convert it to an optimal solution.
In actual interviews, should we just explain the brute force solution and code only the optimal solution or try to code both?@@nikoo28
Nice explanation, thanks man.
Thank you so much sir
Very Simple and clear explaination , Thank you ❤
Thank you so much this one was a bit confusing
Thank you brother 🙌
Nice explanation
great explaination. Thank you
Finally was able to understand it😭👍
Excellent narration
love to way of explaination 😍
Amazing Solution. Thanks a lot.
Please make a video on Repeated String Match question
Asusal nikil is on fire❤
I can't think... Thanku for this optimal approch
very nicely explained. thanks
But I think we can also do without sorting bcs ur comparing first and last word of the list and also how is the space complexity o(1) when ur appending
Plus sorting itself will take o(n)
Plus iteration of first word will take o(length of the word)
SC will be O(n) right for using stringbuilder?? Great explanation though
you are absolutely correct. My bad..the space complexity will be O(n)
wow what an explanation
Thanks for the clear explanation. but i have solved using php😊
Hey Bro really thanks, wanna give a suggestion, please use dark color in ppts.
i prefer while, as it gives an experience with whiteboarding as well. You will be doing a lot of whiteboarding in any organization. 😄
This is a great solution.
Thank you
Thank you so much!!!
nice job
Woww explained so well.. just loved it 🥰❤️
You explain nicely!
Really good.😉
Great explanation! Thank you. :)
Maza aayega ❤
very nicely explained bro
🤝
Thanks
thats great one, but you should also discuss optimised sol also
The optimised solution starts at 07:10
Thanks for solution.
Amazing explanation!!!
In the Arrays.sort method going to iterate every element so we can iterate these elements we can avoid use of Arrays.sort method 😊
Nice Solution
*Excellent Work 🔥Thanks Sir*
Best explanation 💜
Glad that you feel this way :)
Thankyou soo much buddy ❤️
ty for video, great explanation!
don't you think that for loop should also have the condition "&& i < last.lenght()" as if the first array has length greater, then it would also check that index char for the last array, which would result in "IndexArrayOutOfBonds" exception
like the example: canada, car, czech (sorted) here the first element would have the length 6, so the for loop would also run for the value i = 5, but the last[5] doesn't exist
ok so I came up with the answer, its because the traversal of the string would just stop before it hits index more than the last element, correct me if i'm wrong
absolutely correct
Wonderful!!
Awesome👍👍
what is the time complexity of brute force. And Sorting must be also compare all the characters internally then just wondering how it is optimized solution
But i thought Array.sort() will take time complexity of O( n k log(n) ) where n is size of arrays and k is the length of longest string in arrays to sort lexicographical order ... So may it would be too expensive to use it. Now i could store our data in tries format instead of array format. but if we have still the array format then we should use bruteforce approche couse it will take only O(n * k)
Yess!! good observation !!
Worth it !!
i have a query , why we are comparing rhe first and the last string in the efficient solution after sorting the strings .. after sorting even first two strings can give the solution
clever clue deliver
now compare 1st and 2nd 😂
first and the last string will have the maximum possible difference, while first and second will have the minimum difference as they're sorted
if your string str=["clue","clue","clear","clumsy "] then??