← All Tools

Aho-Corasick Multi-Pattern Matcher

Aho-Corasick is the classic algorithm powering grep -F, fgrep, keyword filters, malware scanners, and virus signature databases — it finds every occurrence of every pattern from a dictionary in a single left-to-right pass of the text. The trick is a trie augmented with failure links (like KMP's failure function, extended to a keyword tree) plus optional dictionary links to report shorter suffix matches without re-walking. Complexity: O(|patterns| + |text| + matches).

root   terminal (pattern end)   current state   fail link   dict link

Automaton

Patterns0
Trie states0
Distinct alphabet0
Text length0
Chars consumed0
Fail-link jumps0
Matches found0

Match list

— no matches yet —

Trace

How the automaton is built

Insert every pattern into a trie so shared prefixes reuse states. Then, breadth-first from depth 1, wire each state's fail link: from state u reached by character c off parent p, follow p's fail links until we find one that has a c child — that's fail(u). Because we handle states in BFS order, that link is already computed by the time we need it. During scan, when the current state has no child for the next character, jump via fail links until it does (or we're back at root). Every match is either the pattern ending at the current state or one ending at a state we reach via dictionary links — the shortest chain of fail links to a terminal — so all occurrences are reported without re-scanning any character. That's how the sequence "ushers" reports she, he, and hers in a single pass.