String matching finds occurrences of a pattern inside a text without the naive O(n·m) rescan that re-examines characters after every mismatch. The whole family exists to reuse the work a mismatch reveals: once you know a suffix of what you scanned equals a prefix of the pattern, you can shift by more than one character and never look back.

Two axes separate the members. The first is how many patterns you search for at once — one pattern (KMP, Z-Algorithm, Boyer-Moore) versus a whole set in a single pass (Aho-Corasick). The second is what you preprocess: most methods preprocess the pattern into a failure function or shift table, while Rabin–Karp preprocesses nothing structural and instead fingerprints the text with a rolling hash. That hashing angle is what lets Rabin–Karp scale to many equal-length patterns or extend to 2-D matching where the automaton methods do not.

Diagram

flowchart TD
  A[Match pattern in text] --> B{How many patterns}
  B -->|Many at once| C[Aho-Corasick]
  B -->|One| D{Large alphabet, long pattern}
  D -->|Yes, want sublinear scans| E[Boyer-Moore]
  D -->|No| F{Need guaranteed linear worst case}
  F -->|Prefix-structure problems| G[Z-Algorithm]
  F -->|Streaming or classic linear scan| H[KMP]
  B -->|Many equal-length, or 2-D, or rolling| I[Rabin-Karp]

The family

AlgorithmPatternsPreprocessesTimeWorst caseAux spaceWeaker caseReach for it when
KMPonepattern → prefix (failure) functionO(n + m)O(n + m)Θ(m)Large alphabets, where skipping would payGuaranteed linear scan; never backs up in the text (streaming)
Z-Algorithmonepattern → Z-arrayO(n + m)O(n + m)Θ(n + m)Large text under tight memoryPrefix-structure problems; often the simpler linear method to reason about
Boyer-Mooreonepattern → bad-character + good-suffix tablesO(n / m) bestO(n) with Galil ruleO(m +Σ)
Rabin–Karpone or manytext → rolling hashO(n + m) avgO(n·m) on hash collisionsΘ(m) hash / O(1) scanCollisions or adversarial text → O(n·m)Many equal-length patterns, plagiarism/fingerprinting, 2-D matching
Aho-Corasickmanypattern set → trie + failure linksO(n + Σmᵢ + matches)sameΘ(M·σ) dense / Θ(M) sparseA single pattern; memory-tight dense alphabetsScanning once for a whole dictionary of patterns

The two linear single-pattern methods, KMP and Z-Algorithm, are two views of the same prefix structure: both preprocess the pattern in O(m), guarantee O(n) scans, and are interconvertible. Boyer-Moore gives up the worst-case simplicity to win the average case by skipping ahead — the longer the pattern and the larger the alphabet, the bigger its shifts. Aho-Corasick is KMP generalised from one pattern to a set: the trie holds every pattern and the failure links are the multi-pattern equivalent of KMP’s failure function. Rabin–Karp stands apart — it never builds an automaton, so it stays correct only up to hash collisions, which is exactly why it generalises to problems the automaton methods can’t reach cheaply.

Questions

References

5 items under this folder.