KPM Algorithm
KPM Algorithm
to search for occurrences of a "pattern" string within a "text" string. It was developed by
Donald Knuth, Vaughan Pratt, and James H. Morris in 1977. The key idea behind the KMP
algorithm is to avoid unnecessary comparisons by preprocessing the pattern string.
Key Concepts
1. Preprocessing:
o Construct the LPS array for the pattern string.
2. Search:
o Compare the pattern with the text.
o If there is a mismatch, use the LPS array to decide how far to shift the pattern.
o Repeat until the entire text is scanned or the pattern is found.
Example
Given:
Text: "ABABDABACDABABCABAB"
Pattern: "ABABCABAB"
Advantages
Applications