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 patterns
  • Failure function - Compute failure links using BFS
  • Pattern 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
INPUTALGORITHMRESULTText: "hershey"hersheyPatterns:["he", "she", "his", "hers"]1Build Trie2Add Failure Links3Process Text4Collect MatchesSingle pass through textusing finite automatonMatches Found:["he", 0]["hers", 0]["she", 3]All overlapping matchesfound in single pass💡Key Insight:Failure links in the automaton enable efficient transitions between partial patternmatches without backtracking, allowing single-pass processing of the text.TutorialsPoint - Aho-Corasick Multi-Pattern Search | Optimal Solution
Asked in
Google 45 Microsoft 38 Amazon 32 Facebook 28
23.4K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen