Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/strings/AhoCorasick.java (Java)

Code before:
        }

        public void setPatternInd(int patternInd) {
            this.patternInd = patternInd;
        }
    }

    // Trie Class
    public class Trie {

        private Node root = null; // Root node of the trie

        /*
         * For simplification of methods, declared the below as class attributes
         *
         * res is the list of list containing the indexes of words in patterns/dictionary
         * list index represents word.
         * eg - list[0] contains the list of start-index of word pattern[0]
         *      list[1] contains the list of start-index of word pattern[1]
         *      ......
         *      ......
         *      list[n] contains the list of start-index of word pattern[n]
         */

        private ArrayList<ArrayList<Integer>> res; // Stores positions where patterns are found in the text
        private String[] patterns; // patterns according to which Trie is constructed

        /*
         *  Constructor :
         *        -> Builds Trie according to patterns
         *        -> Builds Suffix Links and OutPut Links
         *
         *  @param patterns : patterns according to which Trie is constructed
         */
        public Trie(String[] patterns) {
            root = new Node(); // Initialize the root of the trie
            res = new ArrayList<>(patterns.length); // Initialize the result data structure
            this.patterns = patterns;
            // Loop through each input pattern
            for (int i = 0; i < patterns.length; i++) {
                res.add(new ArrayList<Integer>()); // Initialize a list to store positions of the current pattern

                Node curr = root; // Start at the root of the trie for each pattern

                // Loop through each character in the current pattern
                for (int j = 0; j < patterns[i].length(); j++) {
                    char c = patterns[i].charAt(j); // Get the current character

                    // Check if the current node has a child for the current character
                    if (curr.getChild().containsKey(c)) {
                        curr = curr.getChild().get(c); // Update the current node to the child node

Review comment (suggestion):
```suggestion
        private final String[] patterns; // patterns according to which Trie is constructed

```

Code after:
        }

        public void setPatternInd(final int patternInd) {
            this.patternInd = patternInd;
        }
    }

    // Trie Class
    public static class Trie {

        private Node root = null; // Root node of the trie
        private final String[] patterns; // patterns according to which Trie is constructed

        public Trie(final String[] patterns) {
            root = new Node(); // Initialize the root of the trie
            this.patterns = patterns;
            buildTrie();
            buildSuffixAndOutputLinks();
        }

        // builds AhoCorasick Trie
        private void buildTrie() {

            // Loop through each input pattern and building Trie
            for (int i = 0; i < patterns.length; i++) {
                Node curr = root; // Start at the root of the trie for each pattern

                // Loop through each character in the current pattern
                for (int j = 0; j < patterns[i].length(); j++) {
                    char c = patterns[i].charAt(j); // Get the current character

                    // Check if the current node has a child for the current character
                    if (curr.getChild().containsKey(c)) {
                        curr = curr.getChild().get(c); // Update the current node to the child node
                    } else {
                        // If no child node exists, create a new one and add it to the current node's children
                        Node nn = new Node();
                        curr.getChild().put(c, nn);
                        curr = nn; // Update the current node to the new child node
                    }
                }
                curr.setPatternInd(i); // Store the index of the pattern in the current leaf node
            }
        }

        private void initializeSuffixLinksForChildNodesOfTheRoot(Queue<Node> q) {
            for (char rc : root.getChild().keySet()) {
                Node childNode = root.getChild().get(rc);
                q.add(childNode); // Add child node to the queue
                childNode.setSuffixLink(root); // Set suffix link to the root
            }