Longest Common Prefix (LeetCode 14) | Full solution with animations and examples | Study Algorithms

  Рет қаралды 108,002

Nikhil Lohia

Nikhil Lohia

Күн бұрын

Пікірлер: 249
@dhritiarora4147
@dhritiarora4147 2 жыл бұрын
All the 15 mins were worth it. Great job!
@nikoo28
@nikoo28 2 жыл бұрын
Thanks for the support and motivation
@SuryaPandey-e2s
@SuryaPandey-e2s Жыл бұрын
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.
@nikoo28
@nikoo28 Жыл бұрын
that is so nice to hear. I hope to keep bringing quality content for you guys.
@PawanKamat-q3i
@PawanKamat-q3i Ай бұрын
I never thought of sorting when I tackled this problem, but this makes so much sense to sort it and compare only two strings instead of all. Loved the approach, shows me how much I still have to learn!
@quantisticnumbers2633
@quantisticnumbers2633 Жыл бұрын
Definitely one of the easiest and best solutions that I came across. Thanks for the video!
@ajaysaran6541
@ajaysaran6541 5 ай бұрын
When i saw the optimized solution, i really amazed. Thanks for helping in learning journey. 15mins completely worth it.
@quartekstudio8127
@quartekstudio8127 11 ай бұрын
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
@officialdreamplayz 11 ай бұрын
how many questions u have conquered till now i m on 4th (easy)
@quartekstudio8127
@quartekstudio8127 11 ай бұрын
@@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
@nikoo28
@nikoo28 10 ай бұрын
all the very best...and glad to see your progress
@kunalkheeva
@kunalkheeva 2 жыл бұрын
your videos seem like bit longer, but its really very productive, I couldnot even skip it for a sec, just great work!! Thank you!
@nikoo28
@nikoo28 2 жыл бұрын
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. 😄
@englishpopop7694
@englishpopop7694 Жыл бұрын
this breakdown made things much more clear and easy to comprehend
@arjunmsit
@arjunmsit 3 ай бұрын
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 🙏🙏
@niteshpandey8207
@niteshpandey8207 Жыл бұрын
Esko dekh ye pta chala ki aise bhi question solve kr sakte hai.... keep posting
@nikoo28
@nikoo28 Жыл бұрын
😄
@tashi7316
@tashi7316 2 жыл бұрын
So far one of the best solution I have seen
@sumanthpai4587
@sumanthpai4587 Жыл бұрын
Very Articulately explained . with proper animations etc , really good job
@HY-eu4ei
@HY-eu4ei 2 жыл бұрын
tried a lot of videos and i would say this was the best explanation of all such a easy and elaborative explanation. thank you
@nikoo28
@nikoo28 2 жыл бұрын
Glad you feel this way ♥️
@enriquegrageda
@enriquegrageda Жыл бұрын
My bro, you are a great teacher!
@sharcodes
@sharcodes Жыл бұрын
Love your Solution. Best Wishes for You. I Would Love to see your Channel Grow, why this is so Underrated.
@nikoo28
@nikoo28 Жыл бұрын
Thanks for your support and motivation. I really hope the youtube algorithm picks up the channel soon 😅
@satyamgupta6030
@satyamgupta6030 Жыл бұрын
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.
@nikoo28
@nikoo28 Жыл бұрын
It's my pleasure
@nginx.h
@nginx.h Жыл бұрын
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
@subhamagarwal476
@subhamagarwal476 8 ай бұрын
addicted to your channel. You explain so simple and in easy way. Thanks a lot
@candichiu7850
@candichiu7850 2 жыл бұрын
Thank you! The BEST on KZbin. Super explanation.Keep up the great work.
@kingop2679
@kingop2679 4 ай бұрын
Your approach is really nice i didnt even think to sort array
@Ishankulkarni
@Ishankulkarni 10 ай бұрын
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)).
@nikoo28
@nikoo28 9 ай бұрын
you are correct...happy to talk about an even more optimal solution if you have to bring up 🙂
@sayednadirofficial
@sayednadirofficial 9 күн бұрын
This was asked to me in an interview
@himadripaul7332
@himadripaul7332 2 жыл бұрын
excellent this channel will grow gradually ..no bakwas straight forward explanation ..
@aryavardhan6807
@aryavardhan6807 Ай бұрын
very nice explanation i am doing dsa in python but i am able to understand procedure very good coded own my own great 15 min worthy
@CostaKazistov
@CostaKazistov 2 жыл бұрын
Great style of teaching algorithms. 10/10 👍
@Prince-fv8mb
@Prince-fv8mb Жыл бұрын
loved the way you explained sir.....This channel is highly underrated
@nikoo28
@nikoo28 Жыл бұрын
Thanks for your support and motivation. I really hope the youtube algorithm picks up the channel soon 😅
@PankajYadav-nz4gn
@PankajYadav-nz4gn 2 жыл бұрын
Oh My GOD!!! I can't imagine that someone can be this good in explanation. Amazing and super thanks.
@nikoo28
@nikoo28 2 жыл бұрын
Wow, thanks!
@kirankutte7073
@kirankutte7073 2 жыл бұрын
Awesome explaination. I liked the slow and animated explainartion. Good job!
@nikoo28
@nikoo28 2 жыл бұрын
Glad you liked it
@kamleshkumarsingh5140
@kamleshkumarsingh5140 11 ай бұрын
SC will be O(n) right for using stringbuilder?? Great explanation though
@nikoo28
@nikoo28 10 ай бұрын
you are absolutely correct. My bad..the space complexity will be O(n)
@nickjr9994
@nickjr9994 Жыл бұрын
Very good explanation brother. This solution was so precise and easy to understand. Thanks a ton
@nikoo28
@nikoo28 Жыл бұрын
glad you liked it.
@egm5081
@egm5081 Жыл бұрын
man, what amazing explanation, thank you so much!
@d54nirajthakare69
@d54nirajthakare69 2 жыл бұрын
The level of Explanation I saw today was amazing !!!
@nikoo28
@nikoo28 2 жыл бұрын
So happy you felt that way
@huquea
@huquea 2 жыл бұрын
OMG, you took teaching algorithm to next level
@nikoo28
@nikoo28 2 жыл бұрын
So happy you feel this way 😄
@spongebobsquarepants4576
@spongebobsquarepants4576 7 ай бұрын
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!
@ronakpatel2826
@ronakpatel2826 5 ай бұрын
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
@tejas_2540
@tejas_2540 12 күн бұрын
Awesome explanation
@LuisGuarin-y4z
@LuisGuarin-y4z Жыл бұрын
You're such a good teacher. Thank you!
@nikoo28
@nikoo28 Жыл бұрын
Glad you think so!
@mdrabby1259
@mdrabby1259 Жыл бұрын
Very good explanation ❤😊
@sandeepkarne8407
@sandeepkarne8407 2 ай бұрын
iam ur new subscriber today nice that i found u in my early prep of time
@nikoo28
@nikoo28 2 ай бұрын
Hope you find the content helpful!
@mohammadaftabansari6882
@mohammadaftabansari6882 26 күн бұрын
Thanks for your guidance. Much appreciated !
@ShiraBoruchovich
@ShiraBoruchovich Жыл бұрын
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)
@nikoo28
@nikoo28 Жыл бұрын
Sure, you can do that
@erenyeager4452
@erenyeager4452 2 жыл бұрын
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.
@zack176
@zack176 Жыл бұрын
Wow so you are finally tired of destroying world
@raghavkaushal7561
@raghavkaushal7561 2 жыл бұрын
Thank you so much brother!!! Your explanation is extremely simple to understand and follow. Wish I had a teacher like you.
@s.harishkumar3087
@s.harishkumar3087 6 ай бұрын
I understand this problem with your explanation Easily❤
@yokeshbl4133
@yokeshbl4133 Ай бұрын
wonderful explanation
@gopro12378
@gopro12378 Жыл бұрын
Crystal clear concept thank you sir!
@CodingJourney-sz3og
@CodingJourney-sz3og Жыл бұрын
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
@officialdreamplayz
@officialdreamplayz 11 ай бұрын
clever clue deliver now compare 1st and 2nd 😂
@rishidangi2978
@rishidangi2978 11 ай бұрын
first and the last string will have the maximum possible difference, while first and second will have the minimum difference as they're sorted
@ankanhaldar1130
@ankanhaldar1130 4 ай бұрын
if your string str=["clue","clue","clear","clumsy "] then??
@bivashroy2829
@bivashroy2829 Жыл бұрын
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
@nafiskamran6900
@nafiskamran6900 Ай бұрын
really good explanation
@chakri2622
@chakri2622 Жыл бұрын
this video made to subscriber to never loss hope and don't give up u will grow
@saiyamjain6099
@saiyamjain6099 2 жыл бұрын
Excellent explanation 🔥 keep making more videos loved it
@rishidangi2978
@rishidangi2978 11 ай бұрын
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
@rishidangi2978
@rishidangi2978 11 ай бұрын
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
@nikoo28
@nikoo28 11 ай бұрын
absolutely correct
@ChaitanyaPimparkar
@ChaitanyaPimparkar 3 жыл бұрын
keep them coming
@jatinkumar4410
@jatinkumar4410 2 жыл бұрын
Hello... Nice and clear explanation. But i have a doubt. Shouldn't the time complexity of efficient approach be: O(n*s*log(n)) (where n: length of array and s: average length of string) Instead of O(nlogn) (13:52) . If that is the case, then time complexity of brute force would be better than this approach. Isn't it? Also aren't we going to consider space complexity taken by sorting algo internally?
@sasidharnaidu4507
@sasidharnaidu4507 10 ай бұрын
Yeah, you are right. The brute force approach is even less time consuming when the initial chars of the words won't match.
@chocochips-as
@chocochips-as Жыл бұрын
Hey Bro really thanks, wanna give a suggestion, please use dark color in ppts.
@nikoo28
@nikoo28 Жыл бұрын
i prefer while, as it gives an experience with whiteboarding as well. You will be doing a lot of whiteboarding in any organization. 😄
@bxp_bass
@bxp_bass 7 ай бұрын
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
@AvinashKumar-ps4tw
@AvinashKumar-ps4tw 2 жыл бұрын
Very Crisp and interesting explanation
@aradhyashukla2813
@aradhyashukla2813 Жыл бұрын
Explained a complex problem very easily 💯👏
@keshavkaushlya8355
@keshavkaushlya8355 Жыл бұрын
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.
@nikoo28
@nikoo28 Жыл бұрын
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.
@saurabhsaxena8470
@saurabhsaxena8470 Жыл бұрын
In actual interviews, should we just explain the brute force solution and code only the optimal solution or try to code both?@@nikoo28
@aparna5778
@aparna5778 9 ай бұрын
Thanks for the explanation. Was easy to follow.
@animationTip
@animationTip 2 жыл бұрын
I love the way of explanation.Thank u so much making such a video bhaiya .
@shivanijain2192
@shivanijain2192 6 ай бұрын
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
@deepti8787
@deepti8787 3 ай бұрын
Nice explanation
@gokul6120
@gokul6120 5 ай бұрын
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)
@ankanhaldar1130
@ankanhaldar1130 4 ай бұрын
Yess!! good observation !!
@amolgawai010
@amolgawai010 7 ай бұрын
Great Explanation, small correction: Space complexity is O(n)
@nikoo28
@nikoo28 7 ай бұрын
we aren't using any extra space to arrive at the solution
@aryasharma69
@aryasharma69 5 ай бұрын
@@nikoo28 but we are using string builder it has the property to store n elements so the overall space complexity will be O(n)
@eyerahulk
@eyerahulk Жыл бұрын
Nice explanation, thanks man.
@VaibhavSingh_code
@VaibhavSingh_code 9 ай бұрын
Maza aayega ❤
@debojitmandal8670
@debojitmandal8670 3 ай бұрын
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)
@amitarajput1153
@amitarajput1153 Жыл бұрын
Best Teacher. Please upload more videos. Super thanks
@Chhooso
@Chhooso Жыл бұрын
Finally was able to understand it😭👍
@mohdsameer2445
@mohdsameer2445 Жыл бұрын
very nicely explained bro
@nikoo28
@nikoo28 Жыл бұрын
🤝
@SaumyaSharma007
@SaumyaSharma007 2 жыл бұрын
Thank you brother 🙌
@LuckyAndDad
@LuckyAndDad 2 жыл бұрын
Excellent narration
@shivenderkumar4521
@shivenderkumar4521 2 жыл бұрын
Very Simple and clear explaination , Thank you ❤
@DalisYn
@DalisYn 2 жыл бұрын
Thank you so much this one was a bit confusing
@curiousvaita
@curiousvaita 8 ай бұрын
Amazing Solution. Thanks a lot.
@muskancreativity7
@muskancreativity7 2 жыл бұрын
Best explanation 💜
@nikoo28
@nikoo28 2 жыл бұрын
Glad that you feel this way :)
@PranitKothari
@PranitKothari Жыл бұрын
You explain nicely!
@SMARTRIXDbztttmods
@SMARTRIXDbztttmods 2 жыл бұрын
*Excellent Work 🔥Thanks Sir*
@denisgordiy5718
@denisgordiy5718 Жыл бұрын
Thank you for great explanation. IMHO more efficiently is not to use StringBulider but just return first.substring(0, index)
@nikoo28
@nikoo28 Жыл бұрын
The difference will be very minute. :)
@palashbharati8818
@palashbharati8818 2 жыл бұрын
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
@oncloudnine4334
@oncloudnine4334 9 ай бұрын
bro can you explain why we go with first and last and explain why can't we go with first and second string
@SouravCheck
@SouravCheck 9 ай бұрын
2:07, A is also a counted in prefix?
@parulsaraogi6770
@parulsaraogi6770 2 жыл бұрын
Woww explained so well.. just loved it 🥰❤️
@gozebe
@gozebe 10 ай бұрын
This is a great solution.
@rishabhjain7651
@rishabhjain7651 Жыл бұрын
love to way of explaination 😍
@kidoo1567
@kidoo1567 Жыл бұрын
Asusal nikil is on fire❤
@rohanyadav5246
@rohanyadav5246 Жыл бұрын
Hi, great video. But after sorting, can't we compare the first and 2nd string in the array. That will also work ? or it will break in any case ?
@nikoo28
@nikoo28 Жыл бұрын
the first and second string will not give the longest common prefix.
@likithganesh9598
@likithganesh9598 Жыл бұрын
​@@nikoo28why it won't give? If it's not present in 2nd string it isn't considered for prefix right?? Could you pls explain
@nikoo28
@nikoo28 11 ай бұрын
Because you need the longest common prefix. Just the first and second gives you the longest prefix. We want a common prefix that is in all the strings.
@salmarafiq7695
@salmarafiq7695 2 жыл бұрын
Amazing explanation!!!
@sahilbait2981
@sahilbait2981 6 ай бұрын
Please make a video on Repeated String Match question
@punamroy6639
@punamroy6639 6 ай бұрын
great explaination. Thank you
@MaksymKondratiuk
@MaksymKondratiuk Жыл бұрын
Thanks for the video. But in the end, it looks a bit confusing. The time complexity of the "brute force" approach should O(n * m) where m is the length of the string and in the case of the "efficient approach" we will have O(n * m * log n) which is slower than the previous one. Is it a case or did I miss something?
@Ishankulkarni
@Ishankulkarni 10 ай бұрын
You are correct
@vm1662
@vm1662 Жыл бұрын
Great explanation! Thank you. :)
@DebsDigitalDiary
@DebsDigitalDiary Жыл бұрын
You are amazing. ❤
@nikoo28
@nikoo28 Жыл бұрын
You're the best!
@ahmed91372
@ahmed91372 Жыл бұрын
What is the time complexity of the brute force approach?
@anshikagarg9485
@anshikagarg9485 2 жыл бұрын
I can't think... Thanku for this optimal approch
@munzirahmed6424
@munzirahmed6424 10 ай бұрын
#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"]))
@hemant8762
@hemant8762 Жыл бұрын
Thank you so much sir
@Abhishek-ji6qj
@Abhishek-ji6qj 2 жыл бұрын
#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)
@TemuriChitashvili
@TemuriChitashvili 10 ай бұрын
Really good.😉
@richieabbugamingff498
@richieabbugamingff498 Ай бұрын
in the time complexity y u dont count the complexity for the loop
@ashish4k07
@ashish4k07 Жыл бұрын
Can O(n) be possible like finding the max lengthed word and least lengthed word and compare them just the doubt
@nikoo28
@nikoo28 Жыл бұрын
Try writing a pseudo code for it
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 839 М.
小丑教训坏蛋 #小丑 #天使 #shorts
00:49
好人小丑
Рет қаралды 54 МЛН
人是不能做到吗?#火影忍者 #家人  #佐助
00:20
火影忍者一家
Рет қаралды 20 МЛН
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
ThePrimeTime
Рет қаралды 809 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 808 М.
Learn JavaScript - Full Course for Beginners
3:26:43
freeCodeCamp.org
Рет қаралды 19 МЛН
Merge Sorted Arrays Without Extra Space | 2 Optimal Solution
32:47
take U forward
Рет қаралды 248 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 209 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 360 М.
小丑教训坏蛋 #小丑 #天使 #shorts
00:49
好人小丑
Рет қаралды 54 МЛН