|
| 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()) { |
| 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(); |
| 77 | + ac.addKeyword("he"); |
| 78 | + ac.addKeyword("she"); |
| 79 | + ac.addKeyword("hers"); |
| 80 | + ac.addKeyword("his"); |
| 81 | + |
| 82 | + ac.buildFailureLinks(); |
| 83 | + |
| 84 | + List<String> matches = ac.search("ushers"); |
| 85 | + System.out.println("Matches found: " + matches); |
| 86 | + } |
| 87 | +} |
0 commit comments