πŸš€Day 5. Search Pattern (KMP Algorithm) 🧠

The problem can be found at the following link: Problem Link

πŸ’‘ Problem Description:

You are given two strings:

  • txt: The text string in which the pattern is to be searched.

  • pat: The pattern string to search for.

The task is to print all indices in txt where pat starts, using 0-based indexing. Return an empty list if no occurrences are found.

πŸ” Example Walkthrough:

Input: txt = "abcab", pat = "ab" Output: [0, 3]

Explanation: The pattern ab appears at indices 0 and 3 in the text string.

Input: txt = "aaaaa", pat = "aa" Output: [0, 1, 2, 3]

Input: txt = "hello", pat = "world" Output: []

Constraints:

  • 1 <= txt.length, pat.length <= 10^6

  • Both strings consist of lowercase English alphabets.

🎯 My Approach:

The Knuth-Morris-Pratt (KMP) algorithm is an efficient pattern matching algorithm that avoids unnecessary comparisons, making it well-suited for this task.

  1. Compute the Longest Prefix Suffix (LPS) Array:

    • Preprocess the pattern string pat to build the LPS array.

    • The LPS array stores the length of the longest prefix which is also a suffix for substrings of pat.

    • This preprocessing helps in skipping characters during comparisons.

  2. Search Using the LPS Array:

    • Traverse the txt and use the pat LPS array to efficiently find matches.

    • If a mismatch occurs, use the LPS array to skip unnecessary comparisons.

  3. Output the Indices:

    • Store the starting indices of matches found in txt.

πŸ•’ Time and Auxiliary Space Complexity

  • Preprocessing (LPS Array): O(m), where m is the length of the pattern string.

  • Searching: O(n), where n is the length of the text string.

  • Overall Time Complexity: O(n + m).

Auxiliary Space Complexity:

  • Space for LPS Array: O(m).

πŸ“ Solution Code

Code (C)

Code (C++)

Code (Java)

Code (Python)

🎯 Contribution and Support:

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


πŸ“Visitor Count

Last updated