MCPcopy Create free account
hub / github.com/TheAlgorithms/Java / AhoCorasick

Class AhoCorasick

src/main/java/com/thealgorithms/strings/AhoCorasick.java:21–246  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19import java.util.Queue;
20
21public final class AhoCorasick {
22 private AhoCorasick() {
23 }
24
25 // Trie Node Class
26 private static class Node {
27 // Represents a character in the trie
28 private final Map<Character, Node> child = new HashMap<>(); // Child nodes of the current node
29 private Node suffixLink; // Suffix link to another node in the trie
30 private Node outputLink; // Output link to another node in the trie
31 private int patternInd; // Index of the pattern that ends at this node
32
33 Node() {
34 this.suffixLink = null;
35 this.outputLink = null;
36 this.patternInd = -1;
37 }
38
39 public Map<Character, Node> getChild() {
40 return child;
41 }
42
43 public Node getSuffixLink() {
44 return suffixLink;
45 }
46
47 public void setSuffixLink(final Node suffixLink) {
48 this.suffixLink = suffixLink;
49 }
50
51 public Node getOutputLink() {
52 return outputLink;
53 }
54
55 public void setOutputLink(final Node outputLink) {
56 this.outputLink = outputLink;
57 }
58
59 public int getPatternInd() {
60 return patternInd;
61 }
62
63 public void setPatternInd(final int patternInd) {
64 this.patternInd = patternInd;
65 }
66 }
67
68 // Trie Class
69 public static class Trie {
70
71 private Node root = null; // Root node of the trie
72 private final String[] patterns; // patterns according to which Trie is constructed
73
74 public Trie(final String[] patterns) {
75 root = new Node(); // Initialize the root of the trie
76 this.patterns = patterns;
77 buildTrie();
78 buildSuffixAndOutputLinks();

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected