Knuth-Morris-Pratt KMP String Matching Algorithm | Search Pattern | GFG POTD

Sdílet
Vložit
  • čas přidán 8. 01. 2024
  • Whatsapp Community Link : www.whatsapp.com/channel/0029...
    This is the 1st Video on our playlist "String Algorithms".
    In this video we will try to understand a very popular string pattern matching Algorithm - "Knuth-Morris-Pratt KMP String Matching Algorithm"
    We will also solve today's GFG POTD using same code of KMP algorithm - "Search Pattern"
    Share your learnings on LinkedIn, Twitter (X), Instagram, Facebook(Meta) with #codestorywithmik & feel free to tag me.
    I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
    We will do live coding after explanation and see if we are able to pass all the test cases.
    Also, please note that my Github solution link below contains both C++ as well as JAVA code.
    Problem Name : Knuth-Morris-Pratt KMP String Matching Algorithm | Search Pattern
    Company Tags : MICROSOFT
    My solutions on Github(C++ & JAVA) : github.com/MAZHARMIK/Intervie...
    Leetcode Link : www.geeksforgeeks.org/problem...
    My DP Concepts Playlist : • Roadmap for DP | How t...
    My Graph Concepts Playlist : • Graph Concepts & Qns -...
    My GitHub Repo for interview preparation : github.com/MAZHARMIK/Intervie...
    Subscribe to my channel : / @codestorywithmik
    Instagram : / codestorywithmik
    Facebook : / 100090524295846
    Twitter : / cswithmik
    Approach Summary : We make use of The Knuth-Morris-Pratt (KMP) algorithm which is an efficient string searching algorithm that precomputes the longest proper prefix which is also a suffix for each prefix of the pattern, stored in an array called LPS. During the search, it uses the LPS array to skip unnecessary character comparisons, resulting in a linear time complexity of O(N + M) for a text of length N and a pattern of length M. KMP is widely used for efficient substring search, particularly in scenarios with large datasets.
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
    ✨ Timelines✨
    00:00 - Introduction
    #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #2024 #newyear

Komentáře • 188

  • @wearevacationuncoverers
    @wearevacationuncoverers Před 6 měsíci +60

    You deserve more subscribers. Thank you for this masterpiece.

    • @codestorywithMIK
      @codestorywithMIK  Před 6 měsíci +10

      Means a lot. Thank you for your kind words 🙏🙏❤️❤️

  • @Thriftinghai
    @Thriftinghai Před 6 měsíci +49

    A suggestion to everyone :
    1. Those who want a crash course on KMP - Abdul Bari Sir's Video is good
    2. Those who want to understand how KMP works and see multiple Dry Runs (Post-mortem of KMP) - Legend MIK is here
    Understanding KMP is easy but to understand the code on how to implement was the toughest part and this 1 hour video was worth watching. This is the only channel where I comment whole heartedly because of the quality of the content and this legend's hard work. Hats off king.

    • @shashankvashishtha4454
      @shashankvashishtha4454 Před 2 měsíci

      i dont think so i dont understand watched this vdo for 3 hours+ still struggling to understand

  • @user-ub2is4rs4x
    @user-ub2is4rs4x Před 6 měsíci +21

    “Dry Run is one of the most underrated skills”
    --- MIK 🔥

  • @user-ub2is4rs4x
    @user-ub2is4rs4x Před 6 měsíci +14

    I think KMP is one of those algorithms jisko samajhna easy hai but code me implement karna very tough. Thanks for explaining code line by line 🙏🏻🙏🏻

  • @aws_handles
    @aws_handles Před 6 měsíci +5

    Wo MIK hai, kuch bhi kar sakta hai 🔥🔥
    I have no words ❤️🙏🏻

  • @RajSingh-te1uo
    @RajSingh-te1uo Před 6 měsíci +25

    1 hour on KMP 😲Thank you so much sir!

  • @souravjoshi2293
    @souravjoshi2293 Před 6 měsíci +3

    "History will remember this legendary explanation of KMP."
    Just completed the video. I was reading comments of others in this video.
    I agree with comments that understanding the concept is easy but being able to code it and explain the intuition and code it up is difficult and you are just marvellous in breaking down a big problem into smaller chunks. And last but not the least, I want this patience level of doing dry runs like you.

  • @ugcwithaddi
    @ugcwithaddi Před 6 měsíci +1

    🔥🔥🙏🏻
    Kisi bhi algorithm ka intuition isi channel par mil sakta hai. Salute to your skill 🫡

  • @IshaanJoshi-ms9mb
    @IshaanJoshi-ms9mb Před 2 měsíci +1

    every second invested was worth it! thanks for helping us out MIK!

  • @ManishKumar-zb2qx
    @ManishKumar-zb2qx Před 5 měsíci +5

    UnderRated channel

  • @salmankhader1258
    @salmankhader1258 Před 6 měsíci +3

    I watched so many videos on kmp but every time i forgot the Algorithm. I find this video as one stop solution. The intuition behind using lps is something which we can expect only from this channel. Thanks a lot.

  • @vanshbaghel5884
    @vanshbaghel5884 Před 6 měsíci +4

    Such a huge difference with your explanation vs other explanations.
    Loving the channel, thanks for everything!!

  • @amannegi6130
    @amannegi6130 Před 6 měsíci +4

    best vedio on KMP👍

  • @arkojeetbera8584
    @arkojeetbera8584 Před 6 měsíci +1

    Legendary explanation 🔥

  • @S3aw0lf
    @S3aw0lf Před 6 měsíci +8

    Was wondering when will u upload this cause had to go thru sm vids to understand KMP but coding it is actually hard

    • @codestorywithMIK
      @codestorywithMIK  Před 6 měsíci +3

      Totally agree. Understanding KMP is easy. But the toughest part is
      - Understanding WHY
      - coding it up
      I hope my video helps 🙏🙏❤️❤️

  • @knowsbetter4113
    @knowsbetter4113 Před 6 měsíci +2

    ❤ crystal clear

  • @gauravbanerjee2898
    @gauravbanerjee2898 Před 6 měsíci +1

    Thanks a lot for your efforts bhaiya ❤

  • @ss8273
    @ss8273 Před 6 měsíci

    most most awaited viedo bhaiya, it would be so so great if you make video on segment trees.

  • @madugulajoshi5050
    @madugulajoshi5050 Před 6 měsíci +2

    Was waiting for this video. Finally dropped. Thank u bro

  • @ChiragWadwa
    @ChiragWadwa Před 6 měsíci +1

    I was waiting for this!❤

  • @prernagupta7729
    @prernagupta7729 Před 6 měsíci +2

    Best video for KMP on youtube !!
    Why couldn't youtube just showed me this video in the beginning 😴??

  • @umeshbisht1054
    @umeshbisht1054 Před 6 měsíci +1

    Thanku so much bhaiya ❤

  • @harchitgulati3065
    @harchitgulati3065 Před 5 měsíci +1

    what an explanation man !

  • @suyashjain3223
    @suyashjain3223 Před 5 měsíci

    Amazing Explanation!!!🔥🔥

  • @KunalSingh-kn2ij
    @KunalSingh-kn2ij Před 5 měsíci +2

    YOU ARE GREAT SIR JI!!🫡

  • @SarthakKumar
    @SarthakKumar Před 6 měsíci +2

    legendary explanation of KMP, after procrastinating for many days finally saw it completely! Great work!

  • @rohitrajput2097
    @rohitrajput2097 Před měsícem

    Kudos to your Hard Work Man

  • @EB-ot8uu
    @EB-ot8uu Před 5 měsíci +1

    I am sure this is the only best detailed explanation on KMP which details out the implementation also line by line. I don't know who you are , but you are doing an amazing work. Hope to collaborate with you someday

  • @nobbiesid1324
    @nobbiesid1324 Před 3 měsíci +1

    Thanku sir
    You are my favourite teacher ❤

  • @sahilanand30
    @sahilanand30 Před 5 měsíci +2

    Nicely explained

  • @chiragsingh8323
    @chiragsingh8323 Před 6 měsíci +1

    Thanks a lot MIK bhai❤

  • @Shivanai_h
    @Shivanai_h Před 6 měsíci +1

    Best explanation on KMP Algo on CZcams.
    Thanks, MIK:)

  • @anshtanwar1813
    @anshtanwar1813 Před 5 měsíci +2

    Really great, worth spending an hour

  • @santoshpathak8201
    @santoshpathak8201 Před 6 měsíci

    Thank-You so Much Brother .

  • @playwithlinux
    @playwithlinux Před 22 dny +1

    Very nice explanation and intuition broh.... You nailed it. 💛

  • @naveroo11
    @naveroo11 Před 5 měsíci

    Very Good Explanation

  • @Mohit_Q
    @Mohit_Q Před 5 měsíci +1

    Waiting for more string algorithm

  • @alfansejigar3636
    @alfansejigar3636 Před měsícem

    you are a very good tutor💓💓

  • @anushkathakur6531
    @anushkathakur6531 Před 5 měsíci

    really loved it😍

  • @CaptainCoder1
    @CaptainCoder1 Před 5 měsíci +1

    Good explanation mik thanks a lot❤❤❤❤❤

  • @Strawcontamination
    @Strawcontamination Před 6 měsíci +1

    Thank you bhaiya

  • @-prachi_pandey..
    @-prachi_pandey.. Před 2 měsíci +1

    Explanation ❤

  • @Pratham_jaiswal
    @Pratham_jaiswal Před 6 měsíci +3

    Thanks a lot bro ❤
    I watched your video of kmp friday 12. Jan 24 and today 14 jan 24 Leetcode WC has 2 question on kmp.😂
    Done both ✅

    • @codestorywithMIK
      @codestorywithMIK  Před 6 měsíci

      Awesome
      So happy to see this comment ❤️❤️❤️🙏🙏🙏

  • @thaman701
    @thaman701 Před 4 měsíci +1

    great sir

  • @user-km4gv6uo9c
    @user-km4gv6uo9c Před 5 měsíci

    bhaiya aapki wajah se easy question me atakne wale ne aaj hard question(3036. Number of Subarrays That Match a Pattern II) bna liya ...thanks for everything in coding bhaiya ...please continue this playlist ...

  • @SouvikAdh
    @SouvikAdh Před 2 měsíci

    Thanks for the video. I finally can understand KMP now. One observation if txt="aaa" and pat="aaaa", your implementation will fail since you didn't add length check of i & j at line no 35 else-if check. Got this test case failure while solving leetcode-28.

  • @Mohit_Q
    @Mohit_Q Před 5 měsíci

    Thank you so much ❤

  • @nani17290
    @nani17290 Před 26 dny +1

    MIK! It's a right number

  • @vivek3247
    @vivek3247 Před 4 měsíci +2

    Sir please came up with all algorithm playlist in one place I am waiting

  • @Ramneet04
    @Ramneet04 Před 6 měsíci +2

    It was the most needed video on CZcams. Thankyou so much ❤❤

    • @codestorywithMIK
      @codestorywithMIK  Před 6 měsíci +1

      My pleasure 😊

    • @Ramneet04
      @Ramneet04 Před 6 měsíci

      @@codestorywithMIK bhaiya can we do length-- instead of length=lps[length-1]???

  • @tutuimam3381
    @tutuimam3381 Před 6 měsíci +1

    Thankyou 👍🎆

  • @atheisth2373
    @atheisth2373 Před 3 měsíci

    very nice

  • @UECAbhishekKumar
    @UECAbhishekKumar Před 5 měsíci +1

    Best explanation ❤

  • @maheshkeshwala62
    @maheshkeshwala62 Před 6 měsíci +1

    love you bro

  • @michaelmaahi7384
    @michaelmaahi7384 Před 6 měsíci +1

    Great bhai

  • @ankitnainwal9714
    @ankitnainwal9714 Před 3 měsíci +1

    Best video on KMP Algorithm 🙌🙌

  • @Neha_Yadav262
    @Neha_Yadav262 Před 6 měsíci

    Explained very well. Can you please upload other string algorithms also.

  • @xiaoshen194
    @xiaoshen194 Před 6 měsíci +4

    Tysm. I have always found KMP and Z algo hard. Hopefully u would cover z algo next. Thnx

  • @prajwalshaw9217
    @prajwalshaw9217 Před 5 měsíci

    Hello sir. Please do make a video on z-algorithm. Your explanations are always the best. Thank u.

  • @user-rp9zm7ew8w
    @user-rp9zm7ew8w Před 6 měsíci +1

    thanx for the video

  • @39_jatinjain4
    @39_jatinjain4 Před 5 měsíci +1

    Superb Explanation 🙂🙂

  • @rugung1381
    @rugung1381 Před 6 měsíci +2

    what a great explanation bhaiya

    • @codestorywithMIK
      @codestorywithMIK  Před 6 měsíci +1

      ❤️🙏😇

    • @rugung1381
      @rugung1381 Před 6 měsíci

      aise proper explanation with step by step intuition bohut kaam milta hai@@codestorywithMIK

  • @codeandtalk6
    @codeandtalk6 Před 6 měsíci +1

    ❤❤❤❤

  • @jr.stark07
    @jr.stark07 Před 4 dny

    Bhaiya, rolling hashing + rabin karp algorithm k uper thi ek vdo banado ! ♥️

  • @phoddaal7130
    @phoddaal7130 Před 5 měsíci +1

    kmp itna dhakad samjhaya hai sir (as always),
    to lage haath Rabin Karp bhi Samjha dijiye 😁😁

  • @namanpadiyar09
    @namanpadiyar09 Před 16 dny +1

    thanks

  • @a3rdtierguy864
    @a3rdtierguy864 Před měsícem +1

    Bhaiya please make video on rabins carp algo

  • @user-lb3iq1ms5l
    @user-lb3iq1ms5l Před 6 měsíci

    Superb bro excellent content, no doubt this one is the best among all.

  • @monikavaid5083
    @monikavaid5083 Před 6 měsíci +1

    Video was extremely good. The only thing that could be added was explaining time complexity after using KMP. Thank you so very much for the best explanation on internet!!!! ☺

    • @codestorywithMIK
      @codestorywithMIK  Před 6 měsíci

      Thank you so much.
      Actually the Time Complexity is O(m+n).
      I will ensure I always add TC and SC after explaining.
      Thank you 🙏😇

    • @monikavaid5083
      @monikavaid5083 Před 6 měsíci

      Thanks a lot! 😊😊

  • @JagannathDebGunin
    @JagannathDebGunin Před 6 měsíci +1

    Waiting for KMP Algorithm dada...
    Thanks a lot...

  • @omgupta7712
    @omgupta7712 Před 6 měsíci +1

    sir ❤❤❤❤

  • @wipeesports8990
    @wipeesports8990 Před 6 měsíci

    Hello bhaiya, please make 1 or 2 long video on recursion and backtracking please. By explaining from 0 to intermediate level. Please 🙏🏻🙏🏻

  • @abhinavnarang4369
    @abhinavnarang4369 Před měsícem

    correction at 37:21 it will be kaash 3 length ka suffix and prefix hota, btw ove your content, top notch

  • @cosmos1749
    @cosmos1749 Před 6 měsíci

    Please explain Rabin karp algo also it will be good in continuation to KMP algo

  • @bhartendupant8859
    @bhartendupant8859 Před 6 měsíci

  • @lofireverbz-wy7go
    @lofireverbz-wy7go Před 5 měsíci +2

    bhaiya please ek video rabin karp par bi bnado , usske 3-4 hard questions ek jaise hai

  • @subhamcoder
    @subhamcoder Před 6 měsíci +2

    Sir codeforces k lya v ak playlist banaya please

  • @user-zt3nh4fq2o
    @user-zt3nh4fq2o Před 5 měsíci

    One significant Question came to my mind is, 22:00 While calculating LPS[2] we took an A as common, but 24:47 while deriving LPS[6] we did not took C as common even though the length was odd in both the cases.

  • @abc-ym4zs
    @abc-ym4zs Před 5 měsíci

    bhaiya can u tell me on which patterns we should concentrate more

  • @Stressful_Jindagi
    @Stressful_Jindagi Před měsícem

    Bhaiya Can you please make video on Rabin Karp and Z algorithm? Pls reply

  • @shobhitsingh8695
    @shobhitsingh8695 Před 10 dny

    bhaiya ek Z Algo pe bhi video bna do please 🙏🙏🙏🙏

  • @Gaurav-vl6ym
    @Gaurav-vl6ym Před 4 dny

    please make video on rabin karp also

  • @vishnu4647
    @vishnu4647 Před 6 měsíci

    can you make a video on leetcode 395 actually i am not able to understand how can we shrink or expand the window.

  • @alfansejigar3636
    @alfansejigar3636 Před měsícem

    can you make a video on leetcode 214. (Shortest Palindrome)

  • @chitranshjain9714
    @chitranshjain9714 Před 3 měsíci

    Iss playlist ko aage badaiye na bhaiya

  • @bhuppidhamii
    @bhuppidhamii Před 5 měsíci +1

    32:09 LPS CODE

  • @nikeshsingh2081
    @nikeshsingh2081 Před 5 měsíci

    You should also pin playlist for this series.

  • @ShivamSingh-xv4rw
    @ShivamSingh-xv4rw Před 6 měsíci

    Sir please make a video on minimum number of coins to make a target dp problem you had already uploaded total ways to make target please sir

  • @Er0Shara
    @Er0Shara Před 6 měsíci +1

    @codestorywithMIk: How about Manacher's Algorithm in coming video, Question related to it, Longest palindromic substring of LC, that would be great!

  • @ashishanubhavmaharana6011
    @ashishanubhavmaharana6011 Před 6 měsíci +1

    Sir if possible could you please make a video on Z-function .

  • @shreya3162
    @shreya3162 Před 3 měsíci

    Sir please make a video on Rabin Karp Plzzzzzzzzzzzzzzzzzz

  • @lalithsharma2259
    @lalithsharma2259 Před 6 měsíci +1

    Self note
    Why pattern[i]=pattern[len] while Finding LPS
    Dry run time stap 34:00-36:10

  • @factinsaan4333
    @factinsaan4333 Před 6 měsíci +4

    Mik>>striver

    • @souravjoshi2293
      @souravjoshi2293 Před 5 měsíci +2

      I think both are equally good.
      But Hard problems me mik sir ko beat nahi kar sakta koi explanation.

  • @ramsankar585
    @ramsankar585 Před 6 měsíci

    The nightmare of my coding career, kitna baar bi karu yaad hi nahi rehta 😅.

  • @vinayjangra1401
    @vinayjangra1401 Před 5 měsíci +3

    why lps for one length is 0?
    see, we are looking for proper prefix (not just prefix), and proper prefix means the prefix can't be equal to the whole string
    so for str = "a", we can't take "a" as a proper prefix, because it's whole string
    But, for str = "aaaa", the lps will be of length = 3, because we can take "aaa_" as the proper prefix which is not whole string and take "_aaa" as the suffix.

  • @AbhinavChandel-zx9oe
    @AbhinavChandel-zx9oe Před 5 měsíci +1

    brother i am just confused in only one paert that why did you put len = lps[len-1] when there is no match during a lps array

    • @codestorywithMIK
      @codestorywithMIK  Před 5 měsíci

      You can do len- - only as well.
      It will work the same way.
      The motive is to try with shorter length if no match.

  • @santoshpathak8201
    @santoshpathak8201 Před 5 měsíci

    Brother can u Help me to Understand the Concept behind the Question "Multiply Strings" Leetcode 43
    we will veryThankfull .

  • @anushkathakur6531
    @anushkathakur6531 Před 5 měsíci +1

    bhaiya, 25:30 time stamp par 6th index mein 4 hona chahiya na?? aaac == caaa

  • @harchitgulati3065
    @harchitgulati3065 Před 6 měsíci +1

    pls explain how in LPS[6]=aaacaaa the longest length of equal suffix and prefix can be 4 by including 'c' as a common element but why sir has not included ?

    • @codestorywithMIK
      @codestorywithMIK  Před 6 měsíci +2

      prefix = “aaac”
      Suffix = “caaa”
      Both are not equal ❤️

  • @dhairyachauhan6622
    @dhairyachauhan6622 Před 6 měsíci +1

    Bro when you get time please answer my question I asked on today's leetcode video :)