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

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 พ.ย. 2024

ความคิดเห็น • 236

  • @dhritiarora4147
    @dhritiarora4147 2 ปีที่แล้ว +83

    All the 15 mins were worth it. Great job!

    • @nikoo28
      @nikoo28  2 ปีที่แล้ว +2

      Thanks for the support and motivation

  • @SuryaPandey-e2s
    @SuryaPandey-e2s ปีที่แล้ว +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  11 หลายเดือนก่อน +3

      that is so nice to hear. I hope to keep bringing quality content for you guys.

  • @arjunmsit
    @arjunmsit หลายเดือนก่อน

    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 🙏🙏

  • @quantisticnumbers2633
    @quantisticnumbers2633 ปีที่แล้ว +8

    Definitely one of the easiest and best solutions that I came across. Thanks for the video!

  • @ajaysaran6541
    @ajaysaran6541 3 หลายเดือนก่อน

    When i saw the optimized solution, i really amazed. Thanks for helping in learning journey. 15mins completely worth it.

  • @englishpopop7694
    @englishpopop7694 ปีที่แล้ว +1

    this breakdown made things much more clear and easy to comprehend

  • @niteshpandey8207
    @niteshpandey8207 ปีที่แล้ว +1

    Esko dekh ye pta chala ki aise bhi question solve kr sakte hai.... keep posting

    • @nikoo28
      @nikoo28  ปีที่แล้ว +1

      😄

  • @sumanthpai4587
    @sumanthpai4587 11 หลายเดือนก่อน +1

    Very Articulately explained . with proper animations etc , really good job

  • @quartekstudio8127
    @quartekstudio8127 9 หลายเดือนก่อน +4

    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 9 หลายเดือนก่อน

      how many questions u have conquered till now i m on 4th (easy)

    • @quartekstudio8127
      @quartekstudio8127 9 หลายเดือนก่อน +1

      @@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  8 หลายเดือนก่อน +2

      all the very best...and glad to see your progress

  • @nginx.h
    @nginx.h ปีที่แล้ว +1

    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

  • @tashi7316
    @tashi7316 2 ปีที่แล้ว +1

    So far one of the best solution I have seen

  • @kingop2679
    @kingop2679 2 หลายเดือนก่อน

    Your approach is really nice i didnt even think to sort array

  • @satyamgupta6030
    @satyamgupta6030 ปีที่แล้ว +6

    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  ปีที่แล้ว +1

      It's my pleasure

  • @enriquegrageda
    @enriquegrageda 9 หลายเดือนก่อน +2

    My bro, you are a great teacher!

  • @HY-eu4ei
    @HY-eu4ei 2 ปีที่แล้ว +1

    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 ♥️

  • @subhamagarwal476
    @subhamagarwal476 5 หลายเดือนก่อน

    addicted to your channel. You explain so simple and in easy way. Thanks a lot

  • @kunalkheeva
    @kunalkheeva 2 ปีที่แล้ว +3

    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 ปีที่แล้ว +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. 😄

  • @CostaKazistov
    @CostaKazistov 2 ปีที่แล้ว +4

    Great style of teaching algorithms. 10/10 👍

  • @himadripaul7332
    @himadripaul7332 2 ปีที่แล้ว +2

    excellent this channel will grow gradually ..no bakwas straight forward explanation ..

  • @raghavkaushal7561
    @raghavkaushal7561 2 ปีที่แล้ว +5

    Thank you so much brother!!! Your explanation is extremely simple to understand and follow.
    Wish I had a teacher like you.

  • @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 😅

  • @spongebobsquarepants4576
    @spongebobsquarepants4576 5 หลายเดือนก่อน

    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 2 หลายเดือนก่อน

      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

  • @bivashroy2829
    @bivashroy2829 ปีที่แล้ว +2

    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

  • @candichiu7850
    @candichiu7850 2 ปีที่แล้ว +2

    Thank you! The BEST on TH-cam. Super explanation.Keep up the great work.

  • @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 ปีที่แล้ว +1

      Wow, thanks!

  • @erenyeager4452
    @erenyeager4452 2 ปีที่แล้ว +3

    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

  • @egm5081
    @egm5081 9 หลายเดือนก่อน +1

    man, what amazing explanation, thank you so much!

  • @Ishankulkarni
    @Ishankulkarni 7 หลายเดือนก่อน

    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  7 หลายเดือนก่อน +1

      you are correct...happy to talk about an even more optimal solution if you have to bring up 🙂

  • @ShiraBoruchovich
    @ShiraBoruchovich ปีที่แล้ว +1

    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

  • @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 😅

  • @s.harishkumar3087
    @s.harishkumar3087 3 หลายเดือนก่อน

    I understand this problem with your explanation Easily❤

  • @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.

  • @huquea
    @huquea 2 ปีที่แล้ว +1

    OMG, you took teaching algorithm to next level

    • @nikoo28
      @nikoo28  2 ปีที่แล้ว

      So happy you feel this way 😄

  • @kirankutte7073
    @kirankutte7073 2 ปีที่แล้ว +2

    Awesome explaination. I liked the slow and animated explainartion. Good job!

    • @nikoo28
      @nikoo28  2 ปีที่แล้ว

      Glad you liked it

  • @LuisGuarin-y4z
    @LuisGuarin-y4z ปีที่แล้ว

    You're such a good teacher. Thank you!

    • @nikoo28
      @nikoo28  ปีที่แล้ว

      Glad you think so!

  • @bxp_bass
    @bxp_bass 4 หลายเดือนก่อน

    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

  • @gopro12378
    @gopro12378 ปีที่แล้ว +1

    Crystal clear concept thank you sir!

  • @palashbharati8818
    @palashbharati8818 2 ปีที่แล้ว +6

    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 7 หลายเดือนก่อน

      bro can you explain why we go with first and last and explain why can't
      we go with first and second string

  • @mdrabby1259
    @mdrabby1259 ปีที่แล้ว +1

    Very good explanation ❤😊

  • @AvinashKumar-ps4tw
    @AvinashKumar-ps4tw ปีที่แล้ว +1

    Very Crisp and interesting explanation

  • @chakri2622
    @chakri2622 ปีที่แล้ว

    this video made to subscriber to never loss hope and don't give up u will grow

  • @aradhyashukla2813
    @aradhyashukla2813 ปีที่แล้ว

    Explained a complex problem very easily 💯👏

  • @saiyamjain6099
    @saiyamjain6099 2 ปีที่แล้ว +3

    Excellent explanation 🔥 keep making more videos loved it

  • @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

  • @amolgawai010
    @amolgawai010 5 หลายเดือนก่อน

    Great Explanation, small correction: Space complexity is O(n)

    • @nikoo28
      @nikoo28  4 หลายเดือนก่อน

      we aren't using any extra space to arrive at the solution

    • @aryasharma69
      @aryasharma69 3 หลายเดือนก่อน

      @@nikoo28 but we are using string builder it has the property to store n elements so the overall space complexity will be O(n)

  • @ChaitanyaPimparkar
    @ChaitanyaPimparkar 2 ปีที่แล้ว +3

    keep them coming

  • @aparna5778
    @aparna5778 7 หลายเดือนก่อน

    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 .

  • @Abhishek-ji6qj
    @Abhishek-ji6qj ปีที่แล้ว +1

    #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)

  • @munzirahmed6424
    @munzirahmed6424 8 หลายเดือนก่อน

    #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"]))

  • @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

  • @eyerahulk
    @eyerahulk ปีที่แล้ว

    Nice explanation, thanks man.

  • @hemant8762
    @hemant8762 ปีที่แล้ว +1

    Thank you so much sir

  • @shivenderkumar4521
    @shivenderkumar4521 2 ปีที่แล้ว

    Very Simple and clear explaination , Thank you ❤

  • @DalisYn
    @DalisYn ปีที่แล้ว

    Thank you so much this one was a bit confusing

  • @SaumyaSharma007
    @SaumyaSharma007 2 ปีที่แล้ว +1

    Thank you brother 🙌

  • @deepti8787
    @deepti8787 23 วันที่ผ่านมา

    Nice explanation

  • @punamroy6639
    @punamroy6639 4 หลายเดือนก่อน

    great explaination. Thank you

  • @Chhooso
    @Chhooso 10 หลายเดือนก่อน

    Finally was able to understand it😭👍

  • @LuckyAndDad
    @LuckyAndDad ปีที่แล้ว

    Excellent narration

  • @rishabhjain7651
    @rishabhjain7651 ปีที่แล้ว

    love to way of explaination 😍

  • @curiousvaita
    @curiousvaita 5 หลายเดือนก่อน

    Amazing Solution. Thanks a lot.

  • @sahilbait2981
    @sahilbait2981 4 หลายเดือนก่อน

    Please make a video on Repeated String Match question

  • @kidoo1567
    @kidoo1567 ปีที่แล้ว

    Asusal nikil is on fire❤

  • @anshikagarg9485
    @anshikagarg9485 2 ปีที่แล้ว

    I can't think... Thanku for this optimal approch

  • @abhijitbiradar
    @abhijitbiradar ปีที่แล้ว

    very nicely explained. thanks

  • @debojitmandal8670
    @debojitmandal8670 17 วันที่ผ่านมา

    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)

  • @kamleshkumarsingh5140
    @kamleshkumarsingh5140 9 หลายเดือนก่อน +2

    SC will be O(n) right for using stringbuilder?? Great explanation though

    • @nikoo28
      @nikoo28  8 หลายเดือนก่อน

      you are absolutely correct. My bad..the space complexity will be O(n)

  • @kaustavhalder7263
    @kaustavhalder7263 ปีที่แล้ว

    wow what an explanation

  • @muhammadmahbub6462
    @muhammadmahbub6462 2 หลายเดือนก่อน

    Thanks for the clear explanation. but i have solved using php😊

  • @amitshukla2268
    @amitshukla2268 ปีที่แล้ว

    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. 😄

  • @synctrader
    @synctrader 8 หลายเดือนก่อน

    This is a great solution.

  • @siddhigolatkar8558
    @siddhigolatkar8558 11 หลายเดือนก่อน +1

    Thank you

  • @md.shimul4805
    @md.shimul4805 ปีที่แล้ว +1

    Thank you so much!!!

  • @simo_the_goat
    @simo_the_goat 11 หลายเดือนก่อน +1

    nice job

  • @parulsaraogi6770
    @parulsaraogi6770 2 ปีที่แล้ว

    Woww explained so well.. just loved it 🥰❤️

  • @PranitKothari
    @PranitKothari ปีที่แล้ว

    You explain nicely!

  • @TemuriChitashvili
    @TemuriChitashvili 7 หลายเดือนก่อน +1

    Really good.😉

  • @vm1662
    @vm1662 ปีที่แล้ว

    Great explanation! Thank you. :)

  • @VaibhavSingh-kh3jr
    @VaibhavSingh-kh3jr 6 หลายเดือนก่อน +1

    Maza aayega ❤

  • @mohdsameer2445
    @mohdsameer2445 ปีที่แล้ว

    very nicely explained bro

  • @sarthakbhargava3514
    @sarthakbhargava3514 ปีที่แล้ว +1

    Thanks

  • @BljtKuntal
    @BljtKuntal ปีที่แล้ว

    thats great one, but you should also discuss optimised sol also

    • @nikoo28
      @nikoo28  ปีที่แล้ว +1

      The optimised solution starts at 07:10

  • @kateach8056
    @kateach8056 ปีที่แล้ว

    Thanks for solution.

  • @salmarafiq7695
    @salmarafiq7695 2 ปีที่แล้ว

    Amazing explanation!!!

  • @myosith4795
    @myosith4795 8 หลายเดือนก่อน

    In the Arrays.sort method going to iterate every element so we can iterate these elements we can avoid use of Arrays.sort method 😊

  • @B_Drive_2K
    @B_Drive_2K ปีที่แล้ว

    Nice Solution

  • @SMARTRIXDbztttmods
    @SMARTRIXDbztttmods ปีที่แล้ว

    *Excellent Work 🔥Thanks Sir*

  • @muskancreativity7
    @muskancreativity7 2 ปีที่แล้ว +1

    Best explanation 💜

    • @nikoo28
      @nikoo28  2 ปีที่แล้ว

      Glad that you feel this way :)

  • @khushaldodeja8267
    @khushaldodeja8267 ปีที่แล้ว

    Thankyou soo much buddy ❤️

  • @colveness9155
    @colveness9155 2 ปีที่แล้ว

    ty for video, great explanation!

  • @rishidangi2978
    @rishidangi2978 9 หลายเดือนก่อน

    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 9 หลายเดือนก่อน +1

      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  9 หลายเดือนก่อน

      absolutely correct

  • @Chathur732
    @Chathur732 2 หลายเดือนก่อน

    Wonderful!!

  • @anshikagarg9485
    @anshikagarg9485 2 ปีที่แล้ว +1

    Awesome👍👍

  • @shivanijain2192
    @shivanijain2192 4 หลายเดือนก่อน +1

    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

  • @gokul6120
    @gokul6120 2 หลายเดือนก่อน +1

    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 หลายเดือนก่อน +1

      Yess!! good observation !!

  • @AmeyBhogaonkar-vy2bx
    @AmeyBhogaonkar-vy2bx 6 หลายเดือนก่อน

    Worth it !!

  • @CodingJourney-sz3og
    @CodingJourney-sz3og 9 หลายเดือนก่อน +1

    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 9 หลายเดือนก่อน +1

      clever clue deliver
      now compare 1st and 2nd 😂

    • @rishidangi2978
      @rishidangi2978 9 หลายเดือนก่อน

      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 หลายเดือนก่อน

      if your string str=["clue","clue","clear","clumsy "] then??