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

Code before:
 *
 * This code implements the Aho-Corasick algorithm, which is used for efficient
 * string matching in a given text. It can find multiple patterns simultaneously
 * and records their positions in the text.
 *
 * Author: Prabhat-Kumar-42
 * GitHub: https://github.com/Prabhat-Kumar-42
 */
public class AhoCorasick {

    // Trie Node Class
    class Node {
        HashMap<Character, Node> child = new HashMap<>();
        Node suffix_link;
        Node output_link;
        int pattern_ind;

        Node() {
            this.suffix_link = null;
            this.output_link = null;
            this.pattern_ind = -1;
        }
    }

    Node root = null; // The root node of the Aho-Corasick trie
    private ArrayList<ArrayList<Integer>> res; // Stores the positions where patterns are found in the text
    private String[] patterns; // patterns according to which Trie is constructed

    // Clears the Aho-Corasick data structures
    public void clear() {
        root = null;
        if (res != null) {
            res.clear();
        }
    }

    // Builds the Aho-Corasick trie from a set of input patterns
    public void buildTrie(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


Review comment (suggestion):
What do you think about storing the result in another data structure, for example:
```suggestion
    private HashMap<String, ArrayList<Integer>> res;
```
so res['somePattern'] would be a list of positions where `somePattern` occurs.

Code after:
 *
 * Author: Prabhat-Kumar-42
 * GitHub: https://github.com/Prabhat-Kumar-42
 */

package com.thealgorithms.strings;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

public final class AhoCorasick {
    private AhoCorasick() {
    }

    // Trie Node Class
    private static class Node {
        // Represents a character in the trie
        private HashMap<Character, Node> child = new HashMap<>(); // Child nodes of the current node
        private Node suffixLink; // Suffix link to another node in the trie
        private Node outputLink; // Output link to another node in the trie
        private int patternInd; // Index of the pattern that ends at this node

        Node() {
            this.suffixLink = null;
            this.outputLink = null;
            this.patternInd = -1;
        }

        public HashMap<Character, Node> getChild() {
            return child;
        }

        public Node getSuffixLink() {
            return suffixLink;
        }

        public void setSuffixLink(final Node suffixLink) {
            this.suffixLink = suffixLink;
        }

        public Node getOutputLink() {
            return outputLink;
        }

        public void setOutputLink(final Node outputLink) {
            this.outputLink = outputLink;
        }