KMP String Matching
Build a prefix-suffix table and replay exact string search with fallback, overlapping matches and a text cursor that never rewinds.
Free theory articleBuild a prefix-suffix table and replay exact string search with fallback, overlapping matches and a text cursor that never rewinds.
Concept & interpretation
The Knuth-Morris-Pratt algorithm finds every exact occurrence of a pattern in a text. It preprocesses the pattern into a table of reusable prefix lengths. After a mismatch, the table identifies the part of the previous match that is still useful, so the text cursor never needs to move backward. A retained border after a full match also preserves overlapping occurrences.