Knuth-Morris-Pratt(KMP) Pattern Matching(Substring search) Part2

Поділитися
Вставка
  • Опубліковано 14 січ 2025

КОМЕНТАРІ • 209

  • @Aditya-mk5sm
    @Aditya-mk5sm 8 років тому +187

    I think this is one of the most required explanations for KMP. It is missing almost everywhere else and the algorithm is being blindly followed incase a mismatch occurs. Thanks a LOT.

    • @punityadav4672
      @punityadav4672 6 років тому

      Pls sir lyrics show in down side b/c problems solution not seeing properly thanking you

    • @lavishgarg4274
      @lavishgarg4274 4 роки тому +1

      @Akhil Raj the time complexity is 0(m+n) as, we are traversing the whole array once ,the the given pattern and the constructed array.
      I think now you have understood .

    • @SearchingPeaceInside
      @SearchingPeaceInside 4 роки тому

      @@lavishgarg4274 But if there are lot of backtraversals in bigger array(worst case scenario), Will it not be: O(m+x). x being number of steps taken back.

    • @sreerampanigrahi
      @sreerampanigrahi 4 роки тому +1

      @@SearchingPeaceInside Hello, The worst case scenario is which it has to back travel in every case, so, in either step we either increase our iterator, or we slide our pattern to be matched. Worst case, you'll slide the pattern once for each iteration, hence this will be O(2*N) which is basically O(N) solution. I hope I'm correct :)

  • @nehaliacharya7257
    @nehaliacharya7257 3 роки тому +9

    Even Abdul Bari didn't explain it in so much detail. Best explanation on the planet!!!! Thank you so much I thought I'll never understand this algorithm but you've made it so easy. Thanks again!

    • @ztexgaming2750
      @ztexgaming2750 Рік тому

      Exactly..this guuuy explained this topic easily without confusion

  • @saranshsawhney6115
    @saranshsawhney6115 9 років тому +38

    Tried like 15 tutorials before this one ! and my search ends :) superb keep it up

  • @CyberrGG
    @CyberrGG 2 роки тому +3

    I've spent more than a week reading articles and watching tutorials to figure out how the hell these values came from but here it is explained in just 9 minutes. Clearest explanation seen so far!!

  • @rakeshvarma8091
    @rakeshvarma8091 11 місяців тому +2

    He's best. I have seen several other videos but none of them has explained why we have to look at previous index and then find out the longest suffix which is also prefix.
    Reason is :
    Let's say we have come across a mismatch at some "j" & "i" , j < i :
    1. character at index "j" is not matching with character at index "i".
    2. We know that the prefix (0, j-1) matches with (i-j, i-1).
    3. Now we have to look at what is the longest suffix which is also prefix for the substring (0, j-1). And this will also be the longest suffix for string ending at index "i-1". This is the trick.
    Thank you Tushar!! You nailed it!! More power to you!!

  • @nikhilnvj
    @nikhilnvj 9 років тому +14

    Way better than other complicated explanations.
    Please make a series on tough interview questions asked in Google, Amazon etc.

  • @Elektroingenieu
    @Elektroingenieu 8 років тому

    I rarely comment.
    "This is by far the best explaination on KMP algorithm I found on UA-cam. And I've watched lots of other videos."

  • @mattcay
    @mattcay 9 років тому

    Thanks for both those videos. YT recommended me this video. I came in only hearing about KMP and 20 minutes later came out with a good grasp of the algorithm. Great work :)

  • @neerajchoudhary3709
    @neerajchoudhary3709 Місяць тому

    Once lived a legend, Tushar Roy

  • @ahsan_kamal
    @ahsan_kamal 9 років тому

    Finally with this video i understood the whole logic behind building LPS[ ] array. Very Clear and Concise explanation.
    Thanks.

  • @malharjajoo7393
    @malharjajoo7393 7 років тому +15

    the interesting thing to notice is that the logic is very similar for when we are creating the preprocessed array and when we are matching the pattern.

  • @fazithfouseen1548
    @fazithfouseen1548 7 років тому +4

    i was totally upset in the part 1 now i am fully cleared....thank you tushar....

  • @julioabcdefghijulio
    @julioabcdefghijulio 3 роки тому

    A great example is always better than a lot of talking information. Thank you!

  • @AlonsoGutierrezCh
    @AlonsoGutierrezCh 7 років тому

    Thanks! This was a great video. Computing the failure function is the trickiest part of the KMP algorithm!

  • @ananyadixit1215
    @ananyadixit1215 4 роки тому

    so lucky to find you...simple and clear way Tushar Sir.

  • @KrishnaKishore
    @KrishnaKishore 9 років тому +3

    Pretty much useful for everything, Competitive Programming and Interviews. :)
    Keep the work going on.

  • @陳翰儒-d5m
    @陳翰儒-d5m 3 роки тому

    Man, you gotta realize that how much you help me, thanks you very much , you're my god!!

  • @balsal8520
    @balsal8520 8 років тому +3

    It's a awesome exaplanation of KMP and i really thank's a lot.

  • @SamrathNagar
    @SamrathNagar 6 років тому

    Thank Tushar sir. You are doing amazing job for us. I go through the many tutorials but didn't understand KMP. Now its completely clear. Thanks a lot.

  • @raghurrai
    @raghurrai 7 років тому

    Way better than any other explanation article or tutorial on the internet (definitely true for a rookie like me).

  • @tejaspandey2333
    @tejaspandey2333 4 роки тому

    Thanks Tushar for a clear and concise video explanation

  • @malharjajoo7393
    @malharjajoo7393 7 років тому

    lol it took me like an entire day to try and figure this out ... thanks a lot for the explanation !

  • @Official-tk3nc
    @Official-tk3nc 4 роки тому +1

    Well done! Gautam Gambir

  • @chenyangwang7232
    @chenyangwang7232 5 років тому

    Finally someone talks about what the number stands for...thank you!

  • @zonghanxu1625
    @zonghanxu1625 9 років тому

    Normally, I don't comment. But you are just so amazing. Plz keep uploading such kind of videos. Thank you so much

  • @adityaparasar3959
    @adityaparasar3959 3 роки тому

    Finding the value of pi for last index clears all d doubts!!!!!!
    themks a lawt!!!

  • @Priyanshgupta1161
    @Priyanshgupta1161 9 років тому

    surely One of the best videos on KMP algo.
    keep it up..

  • @prodev7401
    @prodev7401 9 років тому +20

    please make video on DP bitmasks

  • @Devilfairy1
    @Devilfairy1 7 років тому +1

    This is an explanation that could have only been done if you have deep understanding of KMP. Amazing explanation.

  • @AdityaKumar-be7hx
    @AdityaKumar-be7hx 7 років тому

    Extremely edifying. Keep up the good work.

  • @indiansoftwareengineer4899
    @indiansoftwareengineer4899 4 роки тому +1

    I am wondering why no any new videos are coming up now?
    Great channel.

  • @vaibhavdeshmukh7900
    @vaibhavdeshmukh7900 2 роки тому

    Awesome explanation, Loved it!!!

  • @randomseed42
    @randomseed42 6 років тому

    Excellent explanation! The basic idea is to use the already calculated prefix array (or say Partially Match Table on somewhere) when you are calculating/building the prefix array, if I understand that correctly. Thank you very much!

  • @manjutakhar4496
    @manjutakhar4496 9 років тому

    Best Explanation finally i Got the solution

  • @ashfaqiftakher4486
    @ashfaqiftakher4486 9 років тому

    Great explanation Tushar Roy. Keep uploading awesome tutorials like this one. :)

  • @yv4194
    @yv4194 4 роки тому

    TY for all your videos and my BSC =)

  • @keshavagarwal3981
    @keshavagarwal3981 7 років тому

    Sir a BIG THANKS FROM THAPAR UNIVERSITY, PATIALA. YOU DESERVE A GOLD MEDAL. PLEASE BECOME A PROFESSOR HERE.

    • @267praveen
      @267praveen 3 роки тому

      Degree hogi poori Teri kaka?

  • @higorbotelho3483
    @higorbotelho3483 8 років тому

    You are really helping me study ! Thank you for this.

  • @aliawaiz2178
    @aliawaiz2178 Рік тому

    Sir g kia samhjaya hai
    Me apka bahut bara fan hogya hun

  • @Lisa-kk6go
    @Lisa-kk6go 6 років тому

    The best video I saw about KMP array. It's better if you can give more details about why we handle the case in this way when i and j doesn't match.

  • @medhagautam
    @medhagautam 9 років тому

    nicely explained, i tried it from coreman earlier, but now its more clear to me ... thanks dear :)

  • @debabhishek
    @debabhishek 8 років тому

    thanks Tushar you explained it well.

  • @juand715
    @juand715 2 роки тому

    I love so much, Tushar.

  • @siddharthbidasaria7984
    @siddharthbidasaria7984 7 років тому

    This is great! Helped me a lot. Thanks

  • @pouyajafari9775
    @pouyajafari9775 6 років тому

    your videos are awsome !
    helped me such in graphs and also kmp

  • @dhruva098
    @dhruva098 4 роки тому

    finally I understood why we go to j-1 after looping through 6:08 to 8:30

  • @vardaanbajaj3181
    @vardaanbajaj3181 5 років тому

    Beautifully explained. Thank you so much :)

  • @jamepaladin6033
    @jamepaladin6033 3 роки тому

    Helped me a lot. Thank you!

  • @tarungupta5827
    @tarungupta5827 4 роки тому

    Thank you so much! It helped me a lot.

  • @kidou123456
    @kidou123456 8 років тому +2

    This one is really helpful to understand why j keeps backtracking to j - 1 position! Thanks a ton!

  • @satishreddy4893
    @satishreddy4893 9 років тому

    thank u very much ...great explanation with good examples.

  • @robealamalik7763
    @robealamalik7763 9 років тому

    nice video..it clears my concepts...thanks

  • @SDETAcademy1
    @SDETAcademy1 6 років тому +1

    sir i think last one will 3 instead of 4. sir please explain if i m wrong

  • @vivekmathur2532
    @vivekmathur2532 9 років тому

    Very nicely explained.Thanks sir

  • @jethalalnhk2409
    @jethalalnhk2409 4 роки тому

    your published this video on my birthday...14 june ..

  • @ajitshiva9193
    @ajitshiva9193 4 роки тому

    Finally, My search ends after watching more than 10 videos...

  • @mcparadip
    @mcparadip 6 років тому

    Very helpful video. Thank you!

  • @pooja21790
    @pooja21790 8 років тому +1

    bahut bdiya roy
    bhai

  • @jameskurakula8560
    @jameskurakula8560 3 роки тому

    best explanation ever.

    • @lycan2494
      @lycan2494 3 роки тому

      meh could be better

  • @pritamghosh270
    @pritamghosh270 9 років тому

    thank you sir..ur tutorials helps me lot>>>>...

  • @nischalguruprasad600
    @nischalguruprasad600 8 років тому

    Nice work Tushar!

  • @anyagupta5713
    @anyagupta5713 9 років тому +1

    Sir again awesome video.... sir when you are going to make videos on graph ? eagerly waiting to watch graph videos....

  • @manojyadav6967
    @manojyadav6967 2 роки тому

    excellent expalanation sir

  • @venkatsraghavan1793
    @venkatsraghavan1793 2 місяці тому

    Amazing sir 👏

  • @tibormikita
    @tibormikita 8 років тому

    beautifully explained, thank you sir

  • @pdmang
    @pdmang 8 років тому

    Thank you Tushar !

  • @AndreKhan716
    @AndreKhan716 7 років тому +1

    Thank you! Great job :)

  • @witty_idipt
    @witty_idipt 3 роки тому

    FOR LAST MISTMATCH :
    The first 11 are same with last 11 i.e
    0-10 = 6-16 now if 11 and 17 were same we could say 0-11 = 6 -17 but that's not the case
    so we see how many char are same in first 0-10 which is lps[10] = 5
    so 0-4 = 6-10 or 0-4 = 12 -16 as 6-10 = 12 -16
    so if 5 and 17 were equal we could say 0-5 = 12-17 but again not the case
    so how many are equal in first 0-4 i.e = 3
    so 0-2 = 14-16 and 3 = 17 so 0-3 = 14-17 i.e. 4 length
    ps: Thank you Tushar for a rly good explanation!

  • @yashpradhan1617
    @yashpradhan1617 5 років тому

    Excellent explanation!

  • @manojkumaryadav6603
    @manojkumaryadav6603 8 років тому

    Fabulous explaination !!!

  • @SachinSharma-vd5fr
    @SachinSharma-vd5fr 9 років тому

    and thank you very much for uploading this useful video... :)

  • @sharifabahar6257
    @sharifabahar6257 9 років тому

    Thanks a lot . very very excellent and beneficial .

    • @sharifabahar6257
      @sharifabahar6257 9 років тому

      Hello , May you change it to C++ code ? I mean the thing that you wrote for KMP substring search from a file . It is my DSA group project but i tried too much could not . if u can . thank you so much .

    • @sharifabahar6257
      @sharifabahar6257 9 років тому

      Our group project is about searching sub string from a file using KMP algorithm in c++ .
      I tried a lot to convert your coding from java to c++ but could not . May be i need more time to do it . But i have a lot of things to do from other subjects . If you can plz help us . again tnqs a lot .

    • @sharifabahar6257
      @sharifabahar6257 9 років тому

      ok thanks a lot .

  • @rishabhsharma5050
    @rishabhsharma5050 7 років тому

    this video just helped me...thnx

  • @sumitkesarwani4930
    @sumitkesarwani4930 8 років тому

    Awesome tushar...

  • @marcellooliva786
    @marcellooliva786 9 років тому +1

    Good stuff! At the very end, after you set tmp[17] = 4; do you increment 'j' since the values 'c' match? In other words, is j==4 at the very end? This becomes important if we have a longer input.

  • @comenadh
    @comenadh 4 роки тому

    very nice explanation

  • @deveshmodale9396
    @deveshmodale9396 7 років тому

    really helped me a lot .... thanks bro!!!

  • @chaoli9973
    @chaoli9973 4 роки тому

    Thanks for the nice video man!

  • @abrahammoges1947
    @abrahammoges1947 6 років тому

    waw you give so amuesing tutorial thanks so much!!!!!

  • @teamsarmuliadi6960
    @teamsarmuliadi6960 7 років тому

    You are the best.. Thanks (y)

  • @sampathvinayindraganti8041
    @sampathvinayindraganti8041 Рік тому

    thank you so much clarifying sir

  • @malharjajoo7393
    @malharjajoo7393 8 років тому +3

    Key logic - What happens on a mismatch ?
    What we are really doing when we reset j = A[j-1] is considering prefixes and suffixes of the longest prefix that we found on the first mismatch ... this way we are first considering the longest suffix( thats a prefix ) and then the 2nd longest , and then the 3rd longest and soon until j=0.

    • @yusongshen5016
      @yusongshen5016 8 років тому

      Hi malhar. I am still not quite sure here. Why we can safely reset j = A[j - 1]? 'j' here points to the end of longest prefix (it's also suffix) of s[0:i] (substring we have seen so far). For example, in the last part of video. When we move i from 16 to 17 (j from 10 to 11) and find s[j] != s[i], why we can safely jump from j = 10 to j = A[j - 1] = 5 instead of j = 10 to j = 9? How can we know that j = 6, 7, 8, 9 are all impossible as the candidate prefix length and safely discard them?
      Could you explain this part? Thanks very much!

    • @malharjajoo7393
      @malharjajoo7393 7 років тому

      +yusong shen -
      Ok ,Let me clarify -
      1) I feel your concept of "safety" is slightly incorrect , please correct me if I'm wrong.
      On seeing a mismatch ,"Safety" should mean that we should not skip past any length of the pattern
      and should start at j=0.
      2) According to your question , at j=10 , if you are going to j = 5 , then dont you think that should be "safer" than going to j=9 ?
      3) "Why is it safe to skip past j=0,1,2,3,4" is what I feel you should have asked.
      In short - I feel going backward in the pattern ( and then checking for matching ) should be safer than going forward ( and then checking for matching )

    • @malharjajoo7393
      @malharjajoo7393 7 років тому

      To explain why his method is correct -
      1) When you get a mismatch , if j > 0 , then that means before the mismatch , you matched upto a lenght of pattern "j-1" ( if j > 0 )
      I will refer to j and i as "mismtach index" for now. ... where s[i] ! = s[j].
      2) The suffix of that pattern ( from 0 to j ) is also the prefix of the pattern just before the mismatch index i.
      ( Just see the last bit of the video - I am referrring to "acaca" from index 0 to 4, and 12 to 16.
      3) Hence on seeing the mismatch , from 1) above , you know that you have "safely" matched from index 0 to 4 ( inclusive of 0 and 4 ) in the pattern. This is the info given by preprocessed array.
      why he keeps resetting j = A[j-1] is slightly trickier but intuitively ( and can be understood by drawing it out ) by what I explained in the ansewer above.

    • @rishabhgupta3344
      @rishabhgupta3344 7 років тому

      How do we know that the characters from j=0 to j=4 will match those from j=12 to j=16?

  • @winnering
    @winnering Рік тому

    I wonder if the last index value needs to be calculated, because we always only need the previous index value. Such as in this example, there is no need to figure out the value 4 for index 17.

  • @kunaldev5274
    @kunaldev5274 2 роки тому

    Great Explanantion really

  • @jackblack-mp1kk
    @jackblack-mp1kk 6 років тому

    Thank you very much.謝謝你。

  • @pavankhubani4191
    @pavankhubani4191 9 років тому +1

    Nice explanation...

  • @samaryadav7208
    @samaryadav7208 8 років тому

    as always.. great explaination!!

  • @SreeragNairisawesome
    @SreeragNairisawesome 8 років тому +2

    Police sirens in the background at 3:11 ?? :P Great video btw....thnx

  • @coding_entertainment
    @coding_entertainment Рік тому

    you are superb sir

  • @bansal02
    @bansal02 7 років тому

    Thank you for explaining

  • @Deepakyadav-jr3gk
    @Deepakyadav-jr3gk 7 років тому +1

    Great Explanation.

  • @utuk123
    @utuk123 8 років тому

    +Tushar Roy they are used for polynomial evaluations and related stuff

  • @bodhisattva1924
    @bodhisattva1924 6 років тому +4

    amazing video.
    Question:
    if we do such jumps to find candidate J for each i...
    then how are we saying this Prefix construction is O(n).
    for each i isnt there a possibility that we might do j jumps (till we reach j=0)
    and hence it is O(n^2)

    • @jackyjackson9886
      @jackyjackson9886 5 років тому

      I also has the same question. Can someone explain this please?

  • @SachinSharma-vd5fr
    @SachinSharma-vd5fr 9 років тому

    Is this algorithm is fastest algo to search string pattern...?

  • @sgurjeet99
    @sgurjeet99 6 років тому +1

    3:10 Cops are arriving to learn KMP too.

  • @ButWhyIn2
    @ButWhyIn2 2 роки тому

    Thanks A Lot Sir!!

  • @enditend2
    @enditend2 9 років тому

    can you do the version where you use automata? can't find a decent explanation to that ( how to build it mainly)

  • @boyangdong8927
    @boyangdong8927 8 років тому +1

    You did use a eraser, thanks man

  • @utuk123
    @utuk123 8 років тому

    awesome video Tushar Sir.If u could please give a video on fast fourier transforms and inverse of fft

  • @sumitpawar5698
    @sumitpawar5698 5 років тому

    Thank you so much Sir ...