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 n is the length of the text and m is the length of the pattern.