Pattern Matching
Pattern Matching
2. Search Step:
Using the LPS array, we can avoid unnecessary comparisons while searching for the pattern in the
text. When a mismatch occurs, the LPS array tells us the next position to compare, based on
previously matched characters.
Pre-processing Phase: Compute LPS (Longest Prefix-Suffix) Array
Given the pattern P = {"ABCDABD"} , we create the LPS array for this pattern. Here's how we
compute the LPS array:
- Let’s initialize lps[0] = 0 because there’s no proper prefix for a single character.
- Starting from i = 1 , for each character P[i], check if it matches the character in the pattern at the
position indicated by lps[i-1] . If it does, increment the `lps` value. If not, adjust based on previous
prefix-suffix information.
1. Initialize:
- Pattern: P = {"ABCDABD"}
- Text: S = {"ABC ABCDAB ABCDABCDABDE"}
- LPS Array: [0, 0, 0, 0, 1, 2, 3]
- Text pointer j = 0 (points to the start of the text)
- Pattern pointer i = 0 (points to the start of the pattern)
2. Step-by-step matching:
- Compare S[0] = A with P[0] = A→ match.
- Compare S[1] = B with P[1] = B → match.
- Compare S[2] = C with P[2] = C → match.
- Compare S[3] = A with P[3] = D → mismatch.
Mismatch at P[3] and S[3]:
- The LPS value at P[3] is 0, so we move the pattern pointer i to 0 and text pointer j moves to the
next character.
3. Next comparisons:
- Compare S[1] = B with P[0] = A→ mismatch.
Mismatch at S[1]:
- The LPS value at P[0] is 0, so both the pattern pointer and text pointer move ahead.
4. Step-by-step matching continues, moving through mismatches and using the LPS array to skip over
already matched portions. Eventually, you get:
- First occurrence of pattern at position 15 in the string "ABCDAB".
- Second occurrence of pattern at position 18 in the string "ABCDABCD".