Skip to the workspace
Pebellslearn by building
String Algorithms  ›  Lesson

KMP and the Failure Function

KMP 32 minAfter a mismatch, the pattern's own borders say where to resume, so the text is read once

dsa / KMP and the Failure Function

Explore each scene at your own pace.

01 · What the naive search forgets

What the naive search forgets

KMP and the Failure Function

Definition
The Knuth-Morris-Pratt (KMP) algorithm finds a pattern in a text in O(n + m) time by precomputing, for each prefix of the pattern, its longest border: a shorter prefix that is also a suffix. On a mismatch it falls back to that border instead of restarting.
In plain words
When a match fails partway, you already know what the last few letters of the text were. KMP uses that knowledge to slide the pattern forward by the right amount and keep reading from the same spot, never going back over the text.

In short: KMP precomputes a failure function π, the longest border (a proper prefix that is also a suffix) of every prefix of the pattern, in O(m). On a mismatch the search falls back to the next shorter border instead of restarting, so the text pointer never moves backwards and the whole search is O(n) worst case, guaranteed — unlike Rabin-Karp's hash gamble or the naive scan's O(n · m).

The pattern abcab matched 5 characters then failed; its border ab lets it slide to start 3 and keep reading forward from the same text position

Rabin-Karp makes the windows that don't match cheap, but it is still a gamble. A collision costs a wasted check, and a text full of genuine near-misses can still cost n · m. Quarry has a harder requirement too: it tails live logs. Text arrives a chunk at a time, and once a chunk has gone past, Quarry can't read it again. A matcher that backs up to "try the next alignment" is useless there.

The Knuth-Morris-Pratt algorithm (KMP) fixes both problems. It is exact, it is O(n + m) in the worst case, and its text pointer never moves backwards. The idea behind it is that the naive search keeps throwing away something it already knows.

What the naive search forgets

Search for abcabd in abcabcabd. The first five characters match, and then the text's c meets the pattern's d. The naive search slides the pattern one place right and starts reading again from text index 1.

But at that moment you know the last five text characters: they are abcab, because they just matched the pattern. So you don't need the text to decide which alignments could still work. An alignment can only succeed if the start of the pattern lines up with the end of what you've already matched, which means a prefix of the pattern has to equal a suffix of abcab. The longest such overlap is ab. So slide the pattern until its ab sits under the text's last ab, and carry on from the same text position.

Scene 1 of 12
Project context and further resources
You're building a piece ofQuarry — the text index
This piece — kmp_search(): Finds every occurrence in one forward pass, never re-reading the text.
Scenario Quarry is tailing a service log for the phrase "disk full". Lines arrive faster than they can be stored, so the matcher reads each character once and keeps only how much of the phrase it has matched so far.
kmp_search.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
kmp_search(text, pattern) → listEvery start index of pattern in text, found in one forward pass.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.