Lesson 273

Aho-Corasick

Trie + Failure Links = Multi-Pattern Automaton

1:00

How to match dozens or thousands of patterns in a single left-to-right pass over the text by building a trie augmented with BFS-computed failure links.

By the end, you can

  • Explain why the naive k-pattern search is O(k * n) and how Aho-Corasick improves it.
  • Build a trie for a small pattern set, identifying shared prefixes and endpoint nodes.
  • Define a failure link and state what node it points to.
  • Trace the BFS construction of failure links for a concrete trie.
  • Identify the correct failure-link targets for specific nodes in the {he, she, his, hers} trie.
  • Trace the single-pass scan of "ushers" through the automaton, noting failure-link jumps and output-link reports.
  • Explain what output links do and why they are necessary for overlapping patterns.
  • State the O(n + m + z) complexity and identify the real-world domains where Aho-Corasick is used.
Up next in String Algorithms, Advanced DP & Competitive Techniques
Questions or feedback?