| 54 | |
| 55 | // Step 3: Search the Text |
| 56 | public List<String> search(String text) { |
| 57 | List<String> results = new ArrayList<>(); |
| 58 | TrieNode currentNode = root; |
| 59 | |
| 60 | for (char c : text.toCharArray()) { |
| 61 | while (currentNode != root && !currentNode.children.containsKey(c)) { |
| 62 | currentNode = currentNode.failureLink; |
| 63 | } |
| 64 | if (currentNode.children.containsKey(c)) { |
| 65 | currentNode = currentNode.children.get(c); |
| 66 | } |
| 67 | |
| 68 | // Add matches to results |
| 69 | results.addAll(currentNode.output); |
| 70 | } |
| 71 | |
| 72 | return results; |
| 73 | } |
| 74 | |
| 75 | public static void main(String[] args) { |
| 76 | AhoCorasick ac = new AhoCorasick(); |