Knuth-Morris-Pratt KMP String Matching Algorithm | Search Pattern | GFG POTD
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
You deserve more subscribers. Thank you for this masterpiece.
Means a lot. Thank you for your kind words 🙏🙏❤️❤️
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.
i dont think so i dont understand watched this vdo for 3 hours+ still struggling to understand
“Dry Run is one of the most underrated skills”
--- MIK 🔥
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 🙏🏻🙏🏻
Wo MIK hai, kuch bhi kar sakta hai 🔥🔥
I have no words ❤️🙏🏻
1 hour on KMP 😲Thank you so much sir!
Thank you 😇🙏❤️
it was three hours for me (watched video parts multiple times) and still i dont understand.
True@@shashankvashishtha4454
"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.
🔥🔥🙏🏻
Kisi bhi algorithm ka intuition isi channel par mil sakta hai. Salute to your skill 🫡
every second invested was worth it! thanks for helping us out MIK!
UnderRated channel
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.
❤️❤️🙏🙏
Such a huge difference with your explanation vs other explanations.
Loving the channel, thanks for everything!!
Thank you so much 🙏😇
best vedio on KMP👍
Means a lot to me ❤️🙏🙏🙏
Legendary explanation 🔥
Was wondering when will u upload this cause had to go thru sm vids to understand KMP but coding it is actually hard
Totally agree. Understanding KMP is easy. But the toughest part is
- Understanding WHY
- coding it up
I hope my video helps 🙏🙏❤️❤️
❤ crystal clear
Thanks a lot for your efforts bhaiya ❤
most most awaited viedo bhaiya, it would be so so great if you make video on segment trees.
Was waiting for this video. Finally dropped. Thank u bro
Thank you 😇🙏❤️
I was waiting for this!❤
Thank you 😇🙏❤️
Best video for KMP on youtube !!
Why couldn't youtube just showed me this video in the beginning 😴??
Means a lot 🙏🙏
Thanku so much bhaiya ❤
what an explanation man !
Amazing Explanation!!!🔥🔥
YOU ARE GREAT SIR JI!!🫡
legendary explanation of KMP, after procrastinating for many days finally saw it completely! Great work!
Glad it helped! ❤️❤️🙏🙏
Kudos to your Hard Work Man
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
Thanku sir
You are my favourite teacher ❤
Nicely explained
Thanks a lot MIK bhai❤
Best explanation on KMP Algo on CZcams.
Thanks, MIK:)
Means a lot to me. Thank you so much 🙏😇
Really great, worth spending an hour
Glad you enjoyed it ❤️🙏
Thank-You so Much Brother .
Very nice explanation and intuition broh.... You nailed it. 💛
Very Good Explanation
Waiting for more string algorithm
you are a very good tutor💓💓
really loved it😍
Good explanation mik thanks a lot❤❤❤❤❤
Thank you bhaiya
Explanation ❤
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 ✅
Awesome
So happy to see this comment ❤️❤️❤️🙏🙏🙏
great sir
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 ...
So glad to hear ❤️🙏
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.
Thank you so much ❤
MIK! It's a right number
Sir please came up with all algorithm playlist in one place I am waiting
It was the most needed video on CZcams. Thankyou so much ❤❤
My pleasure 😊
@@codestorywithMIK bhaiya can we do length-- instead of length=lps[length-1]???
Thankyou 👍🎆
very nice
Best explanation ❤
Thanks a lot 😊
love you bro
Great bhai
Thank you 😇🙏❤️
Best video on KMP Algorithm 🙌🙌
Means a lot 🙏
Explained very well. Can you please upload other string algorithms also.
Tysm. I have always found KMP and Z algo hard. Hopefully u would cover z algo next. Thnx
Sure I will 😇❤️🙏
what is z algo?
Hello sir. Please do make a video on z-algorithm. Your explanations are always the best. Thank u.
thanx for the video
Thank you so much for watching 🙏
Superb Explanation 🙂🙂
Thank you 🙂
what a great explanation bhaiya
❤️🙏😇
aise proper explanation with step by step intuition bohut kaam milta hai@@codestorywithMIK
❤❤❤❤
Bhaiya, rolling hashing + rabin karp algorithm k uper thi ek vdo banado ! ♥️
kmp itna dhakad samjhaya hai sir (as always),
to lage haath Rabin Karp bhi Samjha dijiye 😁😁
thanks
Bhaiya please make video on rabins carp algo
Superb bro excellent content, no doubt this one is the best among all.
Thank you so much 🙂🙏❤️
@@codestorywithMIK Boyer Moore Algorithm please
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!!!! ☺
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 🙏😇
Thanks a lot! 😊😊
Waiting for KMP Algorithm dada...
Thanks a lot...
Thank you 😇🙏❤️
sir ❤❤❤❤
Thank you 😇🙏❤️
Hello bhaiya, please make 1 or 2 long video on recursion and backtracking please. By explaining from 0 to intermediate level. Please 🙏🏻🙏🏻
correction at 37:21 it will be kaash 3 length ka suffix and prefix hota, btw ove your content, top notch
❤️❤️❤️
Please explain Rabin karp algo also it will be good in continuation to KMP algo
❤
bhaiya please ek video rabin karp par bi bnado , usske 3-4 hard questions ek jaise hai
In my plan. Soon ❤️🙏
Sir codeforces k lya v ak playlist banaya please
Let me try to take out some time to explore
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.
bhaiya can u tell me on which patterns we should concentrate more
Bhaiya Can you please make video on Rabin Karp and Z algorithm? Pls reply
bhaiya ek Z Algo pe bhi video bna do please 🙏🙏🙏🙏
please make video on rabin karp also
can you make a video on leetcode 395 actually i am not able to understand how can we shrink or expand the window.
can you make a video on leetcode 214. (Shortest Palindrome)
Iss playlist ko aage badaiye na bhaiya
32:09 LPS CODE
You should also pin playlist for this series.
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
@codestorywithMIk: How about Manacher's Algorithm in coming video, Question related to it, Longest palindromic substring of LC, that would be great!
Noted ❤️❤️
Sir if possible could you please make a video on Z-function .
Noted
Sir please make a video on Rabin Karp Plzzzzzzzzzzzzzzzzzz
Self note
Why pattern[i]=pattern[len] while Finding LPS
Dry run time stap 34:00-36:10
Mik>>striver
I think both are equally good.
But Hard problems me mik sir ko beat nahi kar sakta koi explanation.
The nightmare of my coding career, kitna baar bi karu yaad hi nahi rehta 😅.
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.
Perfect 👌👌👌
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
You can do len- - only as well.
It will work the same way.
The motive is to try with shorter length if no match.
Brother can u Help me to Understand the Concept behind the Question "Multiply Strings" Leetcode 43
we will veryThankfull .
bhaiya, 25:30 time stamp par 6th index mein 4 hona chahiya na?? aaac == caaa
aagya samjh....read one of your replies😂❤
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 ?
prefix = “aaac”
Suffix = “caaa”
Both are not equal ❤️
Bro when you get time please answer my question I asked on today's leetcode video :)
Replied ❤️