| 1 | import java.util.*; |
| 2 | |
| 3 | public class AhoCorasick { |
| 4 | |
| 5 | // Trie Node Class |
| 6 | static class TrieNode { |
| 7 | Map<Character, TrieNode> children = new HashMap<>(); |
| 8 | TrieNode failureLink = null; |
| 9 | List<String> output = new ArrayList<>(); |
| 10 | } |
| 11 | |
| 12 | private TrieNode root = new TrieNode(); |
| 13 | |
| 14 | // Step 1: Build the Trie |
| 15 | public void addKeyword(String keyword) { |
| 16 | TrieNode currentNode = root; |
| 17 | for (char c : keyword.toCharArray()) { |
| 18 | currentNode = currentNode.children.computeIfAbsent(c, k -> new TrieNode()); |
| 19 | } |
| 20 | currentNode.output.add(keyword); |
| 21 | } |
| 22 | |
| 23 | // Step 2: Build Failure Links |
| 24 | public void buildFailureLinks() { |
| 25 | Queue<TrieNode> queue = new LinkedList<>(); |
| 26 | root.failureLink = root; |
| 27 | queue.add(root); |
| 28 | |
| 29 | while (!queue.isEmpty()) { |
| 30 | TrieNode current = queue.poll(); |
| 31 | |
| 32 | for (Map.Entry<Character, TrieNode> entry : current.children.entrySet()) { |
| 33 | char c = entry.getKey(); |
| 34 | TrieNode child = entry.getValue(); |
| 35 | |
| 36 | // Set failure link |
| 37 | TrieNode failure = current.failureLink; |
| 38 | while (failure != root && !failure.children.containsKey(c)) { |
| 39 | failure = failure.failureLink; |
| 40 | } |
| 41 | |
| 42 | if (failure.children.containsKey(c) && failure.children.get(c) != child) { |
| 43 | child.failureLink = failure.children.get(c); |
| 44 | } else { |
| 45 | child.failureLink = root; |
| 46 | } |
| 47 | |
| 48 | // Merge output from failure link |
| 49 | child.output.addAll(child.failureLink.output); |
| 50 | queue.add(child); |
| 51 | } |
| 52 | } |
| 53 | } |
| 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()) { |
nothing calls this directly
no outgoing calls
no test coverage detected