LeetCode 14. Longest Common Prefix Solution Explained - Java

Поделиться
HTML-код
  • Опубликовано: 29 окт 2024

Комментарии • 157

  • @atefe3919
    @atefe3919 5 лет назад +128

    No one explains solutions better than you, thank you so much for these videos, they are helping me a lot in the process of getting ready for technical interviews!

  • @joanmoralesf
    @joanmoralesf 2 года назад +14

    Its insane how short this video is but you explained it better than all the long fancy videos. Thank you

  • @jay-wf3ft
    @jay-wf3ft 4 года назад +25

    I think it would be great if you would show 2 solutions. One with a slow run time and then the optimal solution such as this. It really helps to understand it even more. Either way awesome video

  • @KhayamGondal
    @KhayamGondal 2 года назад +5

    you can add a check for prefix length ==0 at the end of the while loop. Incase nothing matches with the 2nd string then no need to check with rest of the list. And instead of substringing every itteration you can keep track of right index and only substring when returning the result.

  • @vcfirefox
    @vcfirefox 3 года назад +3

    Dude, I am probably much older than you, but goddamn I am LOVING your explanations. Getting a chance to prep my interviews very efficiently!

  • @prajwalshenoy9117
    @prajwalshenoy9117 3 года назад +1

    Nice one. We can have a case inside for loop to break it when prefix length reaches zero to avoid continuing to loop when there's no scope of common prefix further.

  • @ananyadutta6358
    @ananyadutta6358 3 года назад +1

    bro your explaining method is quite creative , out of 7 videos based on this that I'd WATCHED , this was the best!

  • @bhargavvaddepally3221
    @bhargavvaddepally3221 4 года назад +12

    Damn man that explanation was so smooth

  • @poojaupadhyay6364
    @poojaupadhyay6364 4 года назад

    such a clear approach! everytime i do leetcode and feel like wtf about a question ,i come here to watch your tutorial and i understand the solution so clearly! thanks a lot,u have a great help dude!

  • @satyamgupta6030
    @satyamgupta6030 Год назад +2

    what a solution man thanks alot nick brother please continue making these videos.
    u r just different when it comes to explaining in most optimised ways.

  • @aadhi_dinesh7668
    @aadhi_dinesh7668 Год назад +1

    IN PYTHON
    s=["flow","flew","flower"]
    k=s[0]
    flag=0
    a=""
    for x in range(len(k)):
    for j in s[1:]:
    if k[x]!=j[x]:
    flag=1
    else:
    a=""+k[x]
    if flag==1:
    break
    else:
    print(a,end="")

  • @6066ChetanKhairnar
    @6066ChetanKhairnar 3 года назад +3

    got through 3 different videos for a good solution but nick is one who always gives the best approach , Appreciate bro

  • @vicente3j
    @vicente3j 2 года назад +1

    When I search up a leetcode question I always go to Nick White if its available 😂 Great video

  • @sreenidhisreesha
    @sreenidhisreesha 2 года назад +2

    Since indexOf is linear time complexity, Doesnt it make this O(n2) time complexity?

  • @simrankak7045
    @simrankak7045 3 года назад

    Why i didnt find your channel until now. Your explaination is crisp clear and very simple thankyou :)

  • @brucewayne9444
    @brucewayne9444 4 года назад +4

    Wow man. I could have never thought of approaching this problem this way. Thanks a ton👍

    • @nurulloabdurakhmanov4914
      @nurulloabdurakhmanov4914 2 года назад

      Exactly man. What should we do to get into thinking like these aproaches ?

    • @ashish4k07
      @ashish4k07 10 месяцев назад

      I thought of using 1st string in array of string as base and scanned through all others and checked if one by one it matches all other strings if it is then we increment the string and hence getting final lcp ans

  • @greatred2558
    @greatred2558 3 года назад

    thanks, tried many things for c# but kept falling out of bounds. This works and simple to understand

  • @utkarshsrivastava3114
    @utkarshsrivastava3114 Год назад

    What a clean and simple approach

  • @hbrymiri8301
    @hbrymiri8301 Год назад

    Hands best step by step explanation.

  • @himanshumalik6913
    @himanshumalik6913 3 года назад

    You know what, you just explained in a so easier way that no one has explained yet.

  • @tanyagoel9140
    @tanyagoel9140 4 года назад +7

    It was so nicely explained.
    Thanks a lot!

  • @prathamchitransh5338
    @prathamchitransh5338 2 года назад

    best explaination for this problem on youtube

  • @headyshotta5777
    @headyshotta5777 2 года назад

    Thanks for the explanation. I still don't really understand how your while loop works but I'll keep working to understand.

  • @carefree_ladka
    @carefree_ladka 2 года назад +1

    indexOf() takes O(N) time . How is your method linear?

  • @divyanshuchaudhari5416
    @divyanshuchaudhari5416 Месяц назад

    Great explanation Nick!! Kudos to you.

  • @saradham2844
    @saradham2844 3 года назад +1

    Best solution for this problem . Great approach and clear explanation Thanks !!!

  • @chloetang211
    @chloetang211 2 года назад +1

    Oh, so prefix means it has to be started at 0. After I knew this, this question became simple. Thank you for your great explanation! love it. Your method is way better than those complex long solutions which I couldn't understand

  • @kshitijpandey9376
    @kshitijpandey9376 3 года назад

    You wrapped that up very smoothly. Thanks Nick🙂.

  • @harshal101096
    @harshal101096 2 года назад +2

    whats the timecomplexity?

  • @Noname-di3yz
    @Noname-di3yz 2 года назад +1

    the only thing I don't understand is why we can set the first element as the prefix initially... isn't it possible that there is a different longest prefix? ex/ ["ab", "cd", "cde", "cdef","cdefg"], where if we set the prefix to "ab" wouldn't your solution return "" ?

    • @syc4654
      @syc4654 Год назад

      probably very late but although the problem doesn't mention it specifically,longest common means that the common prefix must be common for all the elements in the array,so yes,"" would be returned and it would be correct,you can test it yourself inputting your string own string in the Testcase section(In the console) and seeing the expected resulted (from a working code) and your result (from your code)

  • @dgr8raka470
    @dgr8raka470 2 года назад +1

    I think in place of “indexOf” .. we need to use “startswith” method… tq for the vdo

  • @maxstanley1818
    @maxstanley1818 3 года назад +1

    yo but what if the first element in the "fl" prefix array is something like "dog" or something?

  • @tej3679
    @tej3679 2 года назад

    best explanation, you explained clearly kudos

  • @balavardhanreddy8581
    @balavardhanreddy8581 Год назад

    by watching this video i subscribed ur channel

  • @Ben-pb7ct
    @Ben-pb7ct 3 года назад

    Could you explain why the time complexity is O(S), where S is the sum of all characters in all strings?

  • @abdelghafourboufenzi213
    @abdelghafourboufenzi213 8 месяцев назад +1

    but this is incorrect in the case of : "abower","flower","flightwer" the result should be "wer" but the result is empty.

    • @syedrehan3251
      @syedrehan3251 7 месяцев назад

      Bro prefix means starting of the letter

  • @flyfly11111
    @flyfly11111 2 года назад

    You explained it very nicely. Thank you so much

  • @gayan1742
    @gayan1742 2 года назад +1

    Time complexity of indexOf is O(n) though.

  • @ashish4k07
    @ashish4k07 10 месяцев назад

    So can what can be the overall time complexity for this

  • @thomasgeorge4578
    @thomasgeorge4578 3 года назад

    Man! your explanation is just fire 🔥🔥🔥

  • @abhinavs2484
    @abhinavs2484 Год назад

    in worst case it is O^n2 right, is there any better solution!

  • @mohammadosama4709
    @mohammadosama4709 4 года назад

    Why do we need substring method we can check character by character vertically? Wouldn't that me more optimal?

  • @solomonmokua4643
    @solomonmokua4643 2 года назад

    Than you, well explained, was able to implement your solution in Python.

  • @gauravmishra8782
    @gauravmishra8782 Год назад

    how did you came up with this solution?

  • @ТельманБабаев-б4ф

    Amazing explanation!

  • @abhishekmamgain224
    @abhishekmamgain224 2 года назад

    Still you are using While loop and one for-loop. Wouldn't this make the time complexity similar as two for-loops

    • @mohammadrafi895
      @mohammadrafi895 2 года назад

      No, it wouldn't because the inner loop will not depend on the length of input array. Input array could have 100 words but each word can't be longer than 20 characters, on average 5-10.

  • @TheRishit99
    @TheRishit99 2 года назад

    Great explanation. Can solve this with Trie Tree ?

    • @carefree_ladka
      @carefree_ladka 2 года назад

      That's the efficient way to solve this problem

  • @ehsanhosseini5861
    @ehsanhosseini5861 4 года назад

    Thank you for this amazing explanation. What is the time complexity?

    • @royt6924
      @royt6924 3 года назад +1

      I believe it is O(A*S) where A is the iteration for each element of array, where S for the iteration of string

  • @rupaldesai7098
    @rupaldesai7098 5 лет назад +4

    the time complexity is? O(n^2)

    • @NickWhite
      @NickWhite  5 лет назад +6

      Nope because we're not looping through the strs array in the inner loop we're popping characters off of one string

    • @suryaakella4508
      @suryaakella4508 4 года назад +1

      @@NickWhite What if all the elements in the string are same like all flowers than no deletion and the time complexity would be O(n^2)? I think we would have to use trie or suffix array to get O(n)?

    • @i_deepeshmeena
      @i_deepeshmeena 4 года назад +4

      @@suryaakella4508 it will be still O(NM) where M is the length of the longest string in the array

    • @jardondiego
      @jardondiego 4 года назад

      @@i_deepeshmeena Indeed, time complexity is O(MN) and space complexity is O(M).

    • @jardondiego
      @jardondiego 4 года назад

      Time complexity with a Trie (prefix tree) would be O(M log N) where M is the length of the longest string and N the number of strings, right?

  • @Thrallgg
    @Thrallgg 3 года назад +2

    str.indexOf and str.subStr is O(m*n) i think, so I don't think it's optimal.

    • @tuckerleavitt
      @tuckerleavitt 3 года назад

      Yes you're correct, a.indexOf(b) is O(len(a) *len(b)) in the worst case. There is a better solution

    • @pranaychoubey9706
      @pranaychoubey9706 3 года назад

      you can eventually shift to stringbuilder i guess.

  • @davidc3268
    @davidc3268 2 года назад

    how is a linear solution? theres is a nested while loop inside?

  • @zerosandones7547
    @zerosandones7547 2 года назад

    confusion.Cleared(); THANK YOU!

  • @avinashpatil9662
    @avinashpatil9662 2 года назад

    What is the time complexity of this solution?

  • @invictuswildlife
    @invictuswildlife 3 года назад

    Kudos to such a clean explanation ! Thank you.

  • @amitpadaliya6916
    @amitpadaliya6916 4 года назад +1

    really amazing solution

  • @ricardosmith5753
    @ricardosmith5753 2 года назад

    very clean solution.

  • @thatsenoughdixit
    @thatsenoughdixit Год назад

    Very nice. Commenting so I get more videos.

  • @KIM-ODee
    @KIM-ODee 4 месяца назад

    Thanks for your explain!

  • @pragatikumarprusty7061
    @pragatikumarprusty7061 2 года назад

    If possible, please tell the time and space complexity as well ...

  • @pjguitar15
    @pjguitar15 2 года назад +1

    I literally got stuck to this problem and I just gave up and went to RUclips lol

  • @AlejandroRodriguez-lq9mz
    @AlejandroRodriguez-lq9mz 4 года назад

    thanks for sharing such solution, very clear and easy one!

  • @iTzAlwxyss
    @iTzAlwxyss 2 года назад

    How does this not work for processing 4 that uses java ?

  • @makgaboemmanuelmathekga2805
    @makgaboemmanuelmathekga2805 Год назад

    the code breaks though, with index out of bound error when there is no common prefix

  • @freinheit6923
    @freinheit6923 Год назад

    thank you for explaining!

  • @sameer9368
    @sameer9368 5 лет назад +1

    hello....please do the problem on the merging k- sorted array using heap.

    • @NickWhite
      @NickWhite  5 лет назад +1

      send me a link in discord and i'll check it out

    • @sameer9368
      @sameer9368 5 лет назад

      i am solving in eclipse . i have the problem statement. can i share you that problem statement. i dont know the approach how to solve such problem, i am the beginner. also i am unaware of the concept like heap. please do the video on it

  • @adil_k
    @adil_k Год назад

    Thanks for this awesome video

  • @rohitkushwaha6125
    @rohitkushwaha6125 2 года назад

    Thank you so much for the explanation ut was really nice.

  • @abdulqudusyunus9786
    @abdulqudusyunus9786 3 года назад

    Nice explanation

  • @jiangtianfeng5971
    @jiangtianfeng5971 4 года назад +1

    very clear! Thx man

  • @LeetCodeSimplified
    @LeetCodeSimplified 3 года назад

    Great explanation! Thx a lot!!

  • @kso3586
    @kso3586 Год назад

    Amazing! thank you so much👍

  • @Whaleshamu
    @Whaleshamu 5 лет назад +2

    can you make video on Gray code (Leetcode 89)?

    • @NickWhite
      @NickWhite  5 лет назад +1

      I’ll check it out!

  • @srijitdebsarma3451
    @srijitdebsarma3451 2 года назад

    Best explanation 😍🥰

  • @vidhi_bhatt
    @vidhi_bhatt 2 года назад

    simple and on point!

  • @theolim6994
    @theolim6994 Год назад

    Thank you so much for great explanation!!! Helped so much!

  • @shastriswaroop
    @shastriswaroop 4 года назад +1

    How about setting the prefix as the smallest length string and then starting loop from index 0. For longer string array it will be beneficial?

  • @swethanooka896
    @swethanooka896 2 года назад

    Can anyone explain me the while loop part of the code?

  • @sarash9607
    @sarash9607 2 года назад

    Can anyone tell me why do we set the first string as a prefix ? how do we know it is the longest string and the one that has all the letters that we want ?
    what if the array was like strs = [ "flight", "flower", "flow"]

    • @raghavendrathakur2239
      @raghavendrathakur2239 2 года назад

      when program runs it finds common between flight and flower first the common is fl and 2nd iteration it will find common between fl and flow so our aim is to find common in strings so what is there in 1st will be in all the strings if not it returns empty string

  • @shivamd23
    @shivamd23 3 года назад

    Best Solution 😊👍

  • @whiz-code
    @whiz-code 3 месяца назад

    You a genius

  • @Post2prqbu
    @Post2prqbu 4 года назад

    wow! elegant solution!

  • @GoodTimeDev
    @GoodTimeDev 2 года назад

    Thank you !

  • @Itachibatman
    @Itachibatman 2 года назад

    thanks don

  • @NeeruSinghK
    @NeeruSinghK 4 года назад

    great.. nice and easy solution

  • @DevanshuMevada
    @DevanshuMevada 5 лет назад +2

    Thanks Man!

  • @swagatzeher
    @swagatzeher 3 года назад

    Thanks man Nick... you are a pocket sized dynamite ;)

  • @rajeevsingh5453
    @rajeevsingh5453 4 года назад

    What's the time complexity of this algorithm?

  • @habibdurodola5728
    @habibdurodola5728 3 года назад

    Best solutions , Yes you did !

  • @simardeepkaur8275
    @simardeepkaur8275 3 года назад

    Thanks buddy

  • @ruifan793
    @ruifan793 4 года назад

    Help me a lot, thanks

  • @blaze9558
    @blaze9558 Год назад

    thanks man!

  • @gokulkumarbhoomibalan5413
    @gokulkumarbhoomibalan5413 4 года назад

    Really helpful!

  • @fahdagodzo5795
    @fahdagodzo5795 3 года назад

    This guy is smart

  • @jasonliu7186
    @jasonliu7186 4 года назад

    Thanks very much!

  • @xchen7499
    @xchen7499 4 года назад

    Thank you, well explained, and very helpful!

  • @arjun-cw7cr
    @arjun-cw7cr 4 года назад

    thank you

  • @akhila5597
    @akhila5597 3 года назад

    Simply wow

  • @ayushbudhwani
    @ayushbudhwani 3 года назад

    Nailed it!!

  • @MRDELL-mc6zo
    @MRDELL-mc6zo Месяц назад

    Thanks Broh

  • @MdineshKumar-gp9wg
    @MdineshKumar-gp9wg Год назад

    cout