Overview
Knuth-Morris-Pratt preprocessing records how much of the pattern is also a suffix of the matched prefix. On a mismatch, the search resumes from that prefix length instead of restarting from scratch.
Finds pattern matches in text by using a prefix table to skip redundant comparisons.
Compact metadata for implementation and discovery.
Knuth-Morris-Pratt preprocessing records how much of the pattern is also a suffix of the matched prefix. On a mismatch, the search resumes from that prefix length instead of restarting from scratch.
Quick facts
Runtime and memory behavior at a glance.
Best
O(n + m)
Average
O(n + m)
Worst
O(n + m)
Space
O(m)
prefix[i] = length of longest proper prefix that is also a suffix ending at iStores the fallback match length after a mismatch.
Show the pattern index jumping back using the prefix table.
KMP's value is not only linear time, but avoiding repeated rescans of text characters.
What each reference supports for this entry.
String algorithms reference for Knuth-Morris-Pratt's pattern matching, suffix indexing, alignment, or sequence-processing mechanism.
Open sourceTextbook support for Knuth-Morris-Pratt's algorithm family, complexity framing, and standard implementation concepts.
Open sourceGood next jumps from the relationship graph, ranked for useful discovery.
Pattern Matching · Core
KMP uses prefix-function fallback, while Boyer-Moore skips from the pattern's right side.
Compare with thisPattern Matching · Core
Cosine Similarity offers a different exact, approximate, or indexed matching strategy than Knuth-Morris-Pratt.
Compare with thisPattern Matching · Core
Damerau-Levenshtein Distance offers a different exact, approximate, or indexed matching strategy than Knuth-Morris-Pratt.
Compare with thisPattern Matching · Core
Dice Coefficient offers a different exact, approximate, or indexed matching strategy than Knuth-Morris-Pratt.
Compare with thisKMP uses prefix-function fallback, while Boyer-Moore skips from the pattern's right side.
KMP avoids rechecking characters by using prefix-function state.