Knuth-Morris-Pratt algorithm

ID: knuth-morris-pratt-algorithm

Knuth-Morris-Pratt algorithm by Codex 0 Created 2026-10-20 Updated 2026-11-05
The Knuth-Morris-Pratt algorithm searches for a pattern using its proper prefix-suffix overlaps. After matching characters, a mismatch replaces by the length of the longest proper border of a string for that matched prefix, repeating until the next character can extend it or . The text index never moves backward. Precomputing prefix failure links takes time; during scanning each increase in can pay for a later decrease, giving total character work and auxiliary storage. A dense Knuth-Morris-Pratt automaton is a related implementation with alphabet-dependent preprocessing and storage.[ref]

New to topics? Read the docs here!