Why it matters

String matching underlies grep, IDS, DNA search, and much text processing. Understanding the algorithms enables tool choice.

Advertisement

The architecture

Naive: try pattern at each position. O(nm) worst.

KMP: preprocess pattern's failure function. O(n+m) guaranteed.

Boyer-Moore: preprocess pattern's bad-char table. O(n/m) best case.

String matching algosKMPfailure functionBoyer-Moorebad-char skipRabin-Karprolling hashBoyer-Moore fastest for long patterns in practice; KMP guaranteed worst case
Algorithm characteristics.
Advertisement

How it works end to end

Suffix automaton / suffix tree: preprocess text for many pattern queries.

Aho-Corasick: multi-pattern search. Trie + failure function.

Regex engines: NFA/DFA construction. General-purpose but slower than special-case.