Explanation
- The KMP algorithm is used for **pattern matching** in a text. It improves upon the naive approach by avoiding unnecessary re-evaluations of characters.
-
-
Steps
- Preprocess the pattern to create a longest prefix suffix (LPS) array.
- Traverse the text and the pattern while using the LPS array to skip unnecessary comparisons.
-
Time Complexity
- O(n + m)**, where
nis the length of the text andmis the length of the pattern.
- O(n + m)**, where