Overview
The prefix function records, for each position, the length of the longest proper prefix that is also a suffix ending there. This preprocessing is central to KMP-style string matching.
Computes the longest proper border length for every prefix of a string.
Compact metadata for implementation and discovery.
The prefix function records, for each position, the length of the longest proper prefix that is also a suffix ending there. This preprocessing is central to KMP-style string matching.
Quick facts
Runtime and memory behavior at a glance.
Best
O(n)
Average
O(n + m)
Worst
O(n + m)
Space
O(n + m)
Show computing the longest proper border for every prefix on a small pattern preprocessing example.
The prefix function is preprocessing; KMP uses it to decide how far the pattern can shift after a mismatch.
What each reference supports for this entry.
String algorithms reference for Prefix Function's pattern matching, suffix indexing, alignment, or sequence-processing mechanism.
Open sourceTextbook support for Prefix Function's algorithm family, complexity framing, and standard implementation concepts.
Open sourceGood next jumps from the relationship graph, ranked for useful discovery.
Pattern Matching · Core
Prefix Function and Shift-And Algorithm solve related pattern preprocessing problems with different assumptions and tradeoffs.
Compare with thisPrefix Function and Bit-Parallel Levenshtein solve related pattern preprocessing problems with different assumptions and tradeoffs.
Prefix Function and Shift-And Algorithm solve related pattern preprocessing problems with different assumptions and tradeoffs.
Prefix Function and Shift-Or Algorithm solve related pattern preprocessing problems with different assumptions and tradeoffs.