Problems > Dictionary Match Count > Editorial

Dictionary Match Count — Solution & Editorial

Back to the Problem

Aho–Corasick: build a trie of the patterns, add failure links (BFS), accumulate output counts along failure links, then scan T once following goto/fail transitions and summing terminal counts.

Complexity: O(total pattern length + |T|)

Watch out: Output links must be propagated along failure edges — otherwise a pattern that is a suffix of another is missed.