Find a pattern in a text without ever backing up the text pointer. Speed from a precomputed failure table.
lps[i] = longest proper prefix of pattern[:i+1] that is also a suffixlps[j−1] matchedBlue = comparing · green = matched · red = mismatch.
After abab fails, the pattern jumps forward — no rewind.
aaaa…a vs aaaa…b: naive 398K comparisons, KMP 16K → 25× fewer.
Naive isn't wrong — it's fragile (latent quadratic on repetitive input). KMP pays O(m) setup for a linear guarantee, whatever the input. Next: Rabin-Karp — compare numbers (rolling hash), not strings.