Тёмный

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

Nikhil Lohia
Подписаться 46 тыс.
Просмотров 86 тыс.
50% 1

Опубликовано:

 

8 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 226   
@dhritiarora4147
@dhritiarora4147 2 года назад
All the 15 mins were worth it. Great job!
@nikoo28
@nikoo28 2 года назад
Thanks for the support and motivation
@user-kg6gu7re2w
@user-kg6gu7re2w 9 месяцев назад
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 9 месяцев назад
that is so nice to hear. I hope to keep bringing quality content for you guys.
@ajaysaran6541
@ajaysaran6541 23 дня назад
When i saw the optimized solution, i really amazed. Thanks for helping in learning journey. 15mins completely worth it.
@quantisticnumbers2633
@quantisticnumbers2633 Год назад
Definitely one of the easiest and best solutions that I came across. Thanks for the video!
@englishpopop7694
@englishpopop7694 11 месяцев назад
this breakdown made things much more clear and easy to comprehend
@quartekstudio8127
@quartekstudio8127 6 месяцев назад
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 6 месяцев назад
how many questions u have conquered till now i m on 4th (easy)
@quartekstudio8127
@quartekstudio8127 6 месяцев назад
@@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 5 месяцев назад
all the very best...and glad to see your progress
@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
@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. 😄
@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
@sumanthpai4587
@sumanthpai4587 9 месяцев назад
Very Articulately explained . with proper animations etc , really good job
@HY-eu4ei
@HY-eu4ei Год назад
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 Год назад
Glad you feel this way ♥️
@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
@CostaKazistov
@CostaKazistov 2 года назад
Great style of teaching algorithms. 10/10 👍
@subhamagarwal476
@subhamagarwal476 2 месяца назад
addicted to your channel. You explain so simple and in easy way. Thanks a lot
@bxp_bass
@bxp_bass Месяц назад
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
@spongebobsquarepants4576
@spongebobsquarepants4576 2 месяца назад
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
@himadripaul7332
@himadripaul7332 2 года назад
excellent this channel will grow gradually ..no bakwas straight forward explanation ..
@user-fx1kt1ev2b
@user-fx1kt1ev2b Год назад
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
@enriquegrageda
@enriquegrageda 7 месяцев назад
My bro, you are a great teacher!
@tashi7316
@tashi7316 Год назад
So far one of the best solution I have seen
@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 😅
@erenyeager4452
@erenyeager4452 Год назад
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
@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 6 месяцев назад
SC will be O(n) right for using stringbuilder?? Great explanation though
@nikoo28
@nikoo28 5 месяцев назад
you are absolutely correct. My bad..the space complexity will be O(n)
@s.harishkumar3087
@s.harishkumar3087 Месяц назад
I understand this problem with your explanation Easily❤
@niteshpandey8207
@niteshpandey8207 Год назад
Esko dekh ye pta chala ki aise bhi question solve kr sakte hai.... keep posting
@nikoo28
@nikoo28 Год назад
😄
@raghavkaushal7561
@raghavkaushal7561 2 года назад
Thank you so much brother!!! Your explanation is extremely simple to understand and follow. Wish I had a teacher like you.
@punamroy6639
@punamroy6639 Месяц назад
great explaination. Thank you
@candichiu7850
@candichiu7850 2 года назад
Thank you! The BEST on RU-vid. Super explanation.Keep up the great work.
@Ishankulkarni
@Ishankulkarni 4 месяца назад
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 4 месяца назад
you are correct...happy to talk about an even more optimal solution if you have to bring up 🙂
@amolgawai010
@amolgawai010 2 месяца назад
Great Explanation, small correction: Space complexity is O(n)
@nikoo28
@nikoo28 2 месяца назад
we aren't using any extra space to arrive at the solution
@aryasharma69
@aryasharma69 17 дней назад
@@nikoo28 but we are using string builder it has the property to store n elements so the overall space complexity will be O(n)
@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 😅
@egm5081
@egm5081 7 месяцев назад
man, what amazing explanation, thank you so much!
@huquea
@huquea 2 года назад
OMG, you took teaching algorithm to next level
@nikoo28
@nikoo28 2 года назад
So happy you feel this way 😄
@mdrabby1259
@mdrabby1259 Год назад
Very good explanation ❤😊
@d54nirajthakare69
@d54nirajthakare69 2 года назад
The level of Explanation I saw today was amazing !!!
@nikoo28
@nikoo28 2 года назад
So happy you felt that way
@nickjr9994
@nickjr9994 Год назад
Very good explanation brother. This solution was so precise and easy to understand. Thanks a ton
@nikoo28
@nikoo28 Год назад
glad you liked it.
@shivanijain2192
@shivanijain2192 Месяц назад
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
@muhammadmahbub6462
@muhammadmahbub6462 6 дней назад
Thanks for the clear explanation. but i have solved using php😊
@AvinashKumar-ps4tw
@AvinashKumar-ps4tw Год назад
Very Crisp and interesting explanation
@gopro12378
@gopro12378 Год назад
Crystal clear concept thank you sir!
@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 4 месяца назад
bro can you explain why we go with first and last and explain why can't we go with first and second string
@saiyamjain6099
@saiyamjain6099 2 года назад
Excellent explanation 🔥 keep making more videos loved it
@user-ig2kt4jp8i
@user-ig2kt4jp8i 11 месяцев назад
You're such a good teacher. Thank you!
@nikoo28
@nikoo28 10 месяцев назад
Glad you think so!
@munzirahmed6424
@munzirahmed6424 5 месяцев назад
#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"]))
@aparna5778
@aparna5778 4 месяца назад
Thanks for the explanation. Was easy to follow.
@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. :)
@amitarajput1153
@amitarajput1153 Год назад
Best Teacher. Please upload more videos. Super thanks
@kidoo1567
@kidoo1567 10 месяцев назад
Asusal nikil is on fire❤
@aradhyashukla2813
@aradhyashukla2813 Год назад
Explained a complex problem very easily 💯👏
@rishidangi2978
@rishidangi2978 6 месяцев назад
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 6 месяцев назад
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 6 месяцев назад
absolutely correct
@ChaitanyaPimparkar
@ChaitanyaPimparkar 2 года назад
keep them coming
@sarthakbhargava3514
@sarthakbhargava3514 Год назад
Thanks
@Chathur732
@Chathur732 10 дней назад
Wonderful!!
@myosith4795
@myosith4795 5 месяцев назад
In the Arrays.sort method going to iterate every element so we can iterate these elements we can avoid use of Arrays.sort method 😊
@chakri2622
@chakri2622 Год назад
this video made to subscriber to never loss hope and don't give up u will grow
@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 5 месяцев назад
Yeah, you are right. The brute force approach is even less time consuming when the initial chars of the words won't match.
@VaibhavSingh-kh3jr
@VaibhavSingh-kh3jr 4 месяца назад
Maza aayega ❤
@Abhishek-ji6qj
@Abhishek-ji6qj Год назад
#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)
@sahilbait2981
@sahilbait2981 Месяц назад
Please make a video on Repeated String Match question
@curiousvaita
@curiousvaita 3 месяца назад
Amazing Solution. Thanks a lot.
@animationTip
@animationTip 2 года назад
I love the way of explanation.Thank u so much making such a video bhaiya .
@amitshukla2268
@amitshukla2268 10 месяцев назад
Hey Bro really thanks, wanna give a suggestion, please use dark color in ppts.
@nikoo28
@nikoo28 10 месяцев назад
i prefer while, as it gives an experience with whiteboarding as well. You will be doing a lot of whiteboarding in any organization. 😄
@Chhooso
@Chhooso 7 месяцев назад
Finally was able to understand it😭👍
@AmeyBhogaonkar-vy2bx
@AmeyBhogaonkar-vy2bx 3 месяца назад
Worth it !!
@shivenderkumar4521
@shivenderkumar4521 2 года назад
Very Simple and clear explaination , Thank you ❤
@MaksymKondratiuk
@MaksymKondratiuk 7 месяцев назад
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 4 месяца назад
You are correct
@hemant8762
@hemant8762 10 месяцев назад
Thank you so much sir
@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
@subee128
@subee128 8 месяцев назад
Thank you very much
@nikoo28
@nikoo28 8 месяцев назад
Any time
@debasmitadash9974
@debasmitadash9974 10 месяцев назад
You are amazing. ❤
@nikoo28
@nikoo28 10 месяцев назад
You're the best!
@anshikagarg9485
@anshikagarg9485 2 года назад
I can't think... Thanku for this optimal approch
@eyerahulk
@eyerahulk 11 месяцев назад
Nice explanation, thanks man.
@SMARTRIXDbztttmods
@SMARTRIXDbztttmods Год назад
*Excellent Work 🔥Thanks Sir*
@SaumyaSharma007
@SaumyaSharma007 Год назад
Thank you brother 🙌
@voidcrs
@voidcrs 5 месяцев назад
This is a great solution.
@LuckyAndDad
@LuckyAndDad Год назад
Excellent narration
@siddhigolatkar8558
@siddhigolatkar8558 8 месяцев назад
Thank you
@CodingJourney-sz3og
@CodingJourney-sz3og 7 месяцев назад
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 6 месяцев назад
clever clue deliver now compare 1st and 2nd 😂
@rishidangi2978
@rishidangi2978 6 месяцев назад
first and the last string will have the maximum possible difference, while first and second will have the minimum difference as they're sorted
@abhijitbiradar
@abhijitbiradar Год назад
very nicely explained. thanks
@rishabhjain7651
@rishabhjain7651 Год назад
love to way of explaination 😍
@TemuriChitashvili
@TemuriChitashvili 5 месяцев назад
Really good.😉
@simo_the_goat
@simo_the_goat 9 месяцев назад
nice job
@DalisYn
@DalisYn Год назад
Thank you so much this one was a bit confusing
@yashgoel1894
@yashgoel1894 4 месяца назад
class Solution{ String longestCommonPrefix(String arr[], int n){ StringBuilder result=new StringBuilder(); Arrays.sort(arr); char[] first=arr[0].toCharArray(); char[] last=arr[n-1].toCharArray(); for(int i=0; i
@muskancreativity7
@muskancreativity7 2 года назад
Best explanation 💜
@nikoo28
@nikoo28 2 года назад
Glad that you feel this way :)
@mohdsameer2445
@mohdsameer2445 Год назад
very nicely explained bro
@nikoo28
@nikoo28 Год назад
🤝
@md.shimul4805
@md.shimul4805 Год назад
Thank you so much!!!
@parulsaraogi6770
@parulsaraogi6770 Год назад
Woww explained so well.. just loved it 🥰❤️
@vm1662
@vm1662 Год назад
Great explanation! Thank you. :)
@kaustavhalder7263
@kaustavhalder7263 Год назад
wow what an explanation
@PranitKothari
@PranitKothari Год назад
You explain nicely!
@kateach8056
@kateach8056 11 месяцев назад
Thanks for solution.
@devxbasit
@devxbasit 10 месяцев назад
helpful, I just wanted to directly jump to the time complexity timestamp, but could not find it
@nikoo28
@nikoo28 10 месяцев назад
check the video description please.
@anshikagarg9485
@anshikagarg9485 2 года назад
Awesome👍👍
@beb316ratnesh2
@beb316ratnesh2 Год назад
Based on the given constraints for the problem of finding the longest common prefix, let's evaluate the three approaches and determine the most appropriate algorithm: Divide and Conquer: Time Complexity: O(S), where S is the total number of characters in all the strings. Space Complexity: O(M * log N), where M is the length of the longest string and N is the number of strings. Sorting Method: Time Complexity: O(N * M * log N), where N is the number of strings and M is the maximum length of a string. Space Complexity: O(1). First Solution (Nested Loops): Time Complexity: O(N * M), where N is the number of strings and M is the maximum length of a string. Space Complexity: O(1). Considering the constraints of the problem, where the number of strings (N) and the length of strings (M) can be at most 200, all three approaches are feasible. In terms of time complexity, the Divide and Conquer approach has the best complexity of O(S), which is optimal. However, it has a higher space complexity of O(M * log N) due to the recursion stack. The Sorting Method has a time complexity of O(N * M * log N), which is higher than the other two approaches. Additionally, it does not guarantee a correct answer if the sorting affects the relative order of the strings. The First Solution with nested loops has a time complexity of O(N * M), which is slightly better than the Sorting Method. It also has a space complexity of O(1), making it more memory-efficient than the Divide and Conquer approach. Given the constraints and the considerations above, the most appropriate algorithm for finding the longest common prefix among the strings would be the First Solution with nested loops. It provides a good balance between time complexity and space efficiency.
@salmarafiq7695
@salmarafiq7695 2 года назад
Amazing explanation!!!
@colveness9155
@colveness9155 Год назад
ty for video, great explanation!
@khushaldodeja8267
@khushaldodeja8267 10 месяцев назад
Thankyou soo much buddy ❤️
Далее
It was hard 😂
00:15
Просмотров 896 тыс.
OG Buda - Сабака (A.D.H.D)
02:19
Просмотров 134 тыс.
LeetCode was HARD until I Learned these 15 Patterns
13:00
How I would learn Leetcode if I could start over
18:03
Просмотров 490 тыс.
Longest Common Prefix - Leetcode 14 - Python
6:31
Просмотров 168 тыс.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36