This is an implementation of the Aho-Corasick String Matching Dictionary (Wikipedia).
This algorithm lets you search for (all instances of) multiple substrings in a single pass through the haystack. This is done by first compiling the substrings into a finite-state machine (tree) which is fed each haystack character in turn, adjusts its (specific) state, and reports any matches. This runs in .
The implementation is in git and below: