09. Search Pattern (KMP-Algorithm)
Last updated
Last updated
The problem can be found at the following link: Question Link
I have implemented the Knuth-Morris-Pratt (KMP) algorithm for efficient pattern searching in a given text. The algorithm uses a preprocessing step to construct the Longest Prefix Suffix (LPS) array, which is then used during the actual pattern matching.
I created a function getLSP
to calculate the LPS array for the given pattern. This array is crucial for efficient pattern matching.
I implemented the search
function to perform the actual pattern search. It uses the LPS array to optimize the matching process and keeps track of the occurrences of the pattern in the text.
For effective understanding please refer here
Time Complexity: The time complexity of the KMP algorithm is O(m + n)
, where m is the length of the text and n is the length of the pattern. This is because each character of the text and pattern is compared at most twice.
Auxiliary Space Complexity: The space complexity is O(n)
, where n is the length of the pattern. This is due to the LPS array.
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.