On this page
You want every place a pattern of length m occurs in a text of length n. The Knuth–Morris–Pratt (KMP) algorithm does it in O(n + m) time by never re-reading a text character, using a small table built from the pattern alone.
What is the Knuth–Morris–Pratt algorithm?
Donald Knuth, James Morris and Vaughan Pratt described it in "Fast Pattern Matching in Strings", SIAM Journal on Computing, volume 6, 1977. Their abstract says the algorithm finds all occurrences of one string within another "in running time proportional to the sum of the lengths of the strings". The idea is older than the paper: the Wikipedia article on KMP records that Morris conceived it, Knuth found it independently, and Morris and Pratt wrote it up in a 1970 technical report.
KMP reads the text with one pointer that only moves forward. On a mismatch it does not slide back; it asks the table how much of the pattern is still matched.
Why can naive string matching take O(nm) time?
The naive method tries every starting position and compares character by character until a mismatch. Its worst case grows with the product n × m, not the sum the 1977 paper achieves. Take a text of n − 1 A's then a B, and a pattern of m − 1 A's then a B.
There are n − m + 1 shifts. At every shift but the last, m − 1 comparisons succeed and the last fails; at the last shift all m succeed. Either way each shift costs m, so the total is (n − m + 1) × m. For a 1,000,000-character text and a 1,000-character pattern, that is 999,001,000 comparisons. After matching 999 A's, the naive method moves one step right and checks 998 of them again.
How do you compute the prefix function for ABABAC?
The prefix function π[i] is the length of the longest proper prefix of the pattern's first i + 1 characters that is also a suffix of them, as the cp-algorithms guide to the prefix function defines it. "Proper" means shorter than the whole piece. So π records how the pattern overlaps itself.
Work it for ABABAC, positions 0 to 5:
A: nothing shorter to compare, so π[0] = 0.
AB: A is not B, so π[1] = 0.
ABA: A is both a prefix and a suffix, so π[2] = 1.
ABAB: AB is both, so π[3] = 2.
ABABA: ABA is both, so π[4] = 3.
ABABAC: nothing that starts with A can end in C, so π[5] = 0.
So π = [0, 0, 1, 2, 3, 0]. Each new character extends the overlap by one or falls back to a shorter one: at position 5, extending ABA to ABAB fails, then falling back to A (π[2] = 1) and trying AB fails, then trying A fails. The search uses the same fallback chain.
How does a KMP search run, step by step?
The cp-algorithms guide runs the search with the same fallback loop that builds π, continued along the text. Here it is by hand on the text ABABABAC (positions 0 to 7). k counts pattern characters matched so far.
Text 0 to 4 (ABABA) match pattern 0 to 4, so k = 5.
Text 5 is B, pattern 5 is C. Set k = π[4] = 3: the last three characters read, ABA, start the pattern.
Text 5 (B) matches pattern 3 (B), so k = 4.
Text 6 (A) matches pattern 4, so k = 5.
Text 7 (C) matches pattern 5, so k = 6: a match starting at position 2.
That took 9 comparisons and read each of the 8 text characters once. Naive matching takes 13 here: 6 at shift 0, 1 at shift 1, 6 at shift 2.
On the bad case, the pattern of m − 1 A's and a B has π = [0, 1, 2, …, m − 2, 0]. Each mismatch falls back one place, so each text character costs at most two comparisons: about 2 million for the million-character text instead of about a billion.
Why is KMP linear, and how should you practise it?
k rises by at most one per text character, so at most n times. Every fallback lowers k by at least one and k never goes below zero, so there are at most n fallbacks. That caps the search near 2n comparisons, and the same argument caps building π near 2m: the linear bound of the 1977 paper. Big O notation and growth rates explains what O(n + m) means, and the matched length k works like the state of a machine reading one character at a time, which you can see in finite automata and regular languages.
A plan to make it stick:
Compute π by hand for AAAA, ABCABD and AABAAAB (answers: [0, 1, 2, 3], [0, 0, 0, 1, 2, 0], [0, 1, 0, 1, 2, 2, 3]).
Trace ABABAC in ABABAABABAC, where at text position 5 k falls 5, 3, 1, 0 before A matches.
Write the loop yourself and test it on AAAA…AB, counting comparisons.
If prefixes and suffixes feel shaky, the Fermi Concept Library covers every concept in physics, chemistry, biology, mathematics and computing, each with its own lesson and its place in the map. When your hand trace and your code disagree, the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer.



