Aho-Corasick Automaton
Build a trie of patterns, add failure links by BFS, and report overlapping matches during one text scan.
Core idea
Store all patterns in one trie. Failure links connect each state to its longest suffix that is also a trie prefix, so a mismatch reuses text already read instead of restarting every pattern.
Read the visualization
Solid edges form the trie and dashed edges show failures. The active automaton state follows each text character, and output links report overlapping pattern endings.
Insert the next pattern through shared trie-prefix edges.
Complexity and tradeoffs
Time: O(P+n+z). Space: O(P × alphabet) dense. P is total pattern length and z is output count; sparse transitions reduce memory.
Where it fits
Aho-Corasick powers dictionary scanning, moderation filters, intrusion signatures, and lexing when many patterns must be found in the same stream.