Aho-Corasick Multi-Pattern Search - Problem
The Aho-Corasick algorithm is a powerful string-searching algorithm that can search for multiple patterns simultaneously in a text. Unlike naive approaches that search for each pattern individually, Aho-Corasick constructs a finite automaton that processes the text in a single pass.
Given a text string and a list of pattern strings, implement the Aho-Corasick algorithm to find all occurrences of all patterns in the text. Return a list of results, where each result contains the pattern found and its starting position in the text.
Key components:
Trie construction- Build a prefix tree of all patternsFailure function- Compute failure links using BFSPattern matching- Process text using the automaton
Note: The algorithm should handle overlapping matches and multiple patterns ending at the same position.
Input & Output
Example 1 — Basic Multi-Pattern Search
$
Input:
text = "hershey", patterns = ["he", "she", "his", "hers"]
›
Output:
[["he",0],["hers",0],["she",3]]
💡 Note:
Found 'he' at position 0, 'hers' at position 0 (overlapping), and 'she' at position 3. Pattern 'his' not found.
Example 2 — Overlapping Patterns
$
Input:
text = "aaaa", patterns = ["a", "aa", "aaa"]
›
Output:
[["a",0],["a",1],["aa",0],["a",2],["aa",1],["aaa",0],["a",3],["aa",2]]
💡 Note:
All overlapping occurrences are found: single 'a' at each position, 'aa' starting at positions 0,1,2, and 'aaa' at position 0.
Example 3 — No Matches
$
Input:
text = "hello world", patterns = ["xyz", "abc"]
›
Output:
[]
💡 Note:
Neither pattern 'xyz' nor 'abc' exists in the text, so empty result is returned.
Constraints
- 1 ≤ text.length ≤ 104
- 1 ≤ patterns.length ≤ 100
- 1 ≤ patterns[i].length ≤ 100
- text and patterns[i] consist of lowercase English letters only
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code