Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/others/StringMatchFiniteAutomata.java (Java)

Code before:
     * @param text    The text to search within.
     * @param pattern The pattern to search for.
     */
    public static Set<Integer> searchPattern(final String text, final String pattern) {
        final var stateTransitionTable = computeStateTransitionTable(pattern);
        FiniteAutomata finiteAutomata = new FiniteAutomata(stateTransitionTable);

        Set<Integer> indexFound = new TreeSet<>();
        for (int i = 0; i < text.length(); i++) {
            finiteAutomata.consume(text.charAt(i));

            if (finiteAutomata.getState() == pattern.length()) {
                indexFound.add(i - pattern.length() + 1);
            }
        }
        return indexFound;
    }

    /**
     * Computes the finite automata table for the given pattern.
     *
     * @param pattern The pattern to preprocess.
     * @return The state transition table.
     */
    private static int[][] computeStateTransitionTable(final String pattern) {
        int patternLength = pattern.length();
        int[][] stateTransitionTable = new int[patternLength + 1][CHARS];

        for (int state = 0; state <= patternLength; ++state) {
            for (int x = 0; x < CHARS; ++x) {
                stateTransitionTable[state][x] = getNextState(pattern, patternLength, state, x);
            }
        }

        return stateTransitionTable;
    }

    /**
     * Gets the next state for the finite automata.
     *
     * @param pattern       The pattern being matched.
     * @param patternLength The length of the pattern.
     * @param state         The current state.
     * @param x             The current character from the input alphabet.
     * @return The next state.
     */
    private static int getNextState(final String pattern, final int patternLength, final int state, final int x) {
        // If the current state is less than the length of the pattern
        // and the character matches the pattern character, go to the next state
        if (state < patternLength && x == pattern.charAt(state)) {
            return state + 1;

Review comment (suggestion):
```suggestion
        final int patternLength = pattern.length();
```
or simply use `pattern.length()` directly.

Code after:
     * @param text    The text to search within.
     * @param pattern The pattern to search for.
     */
    public static Set<Integer> searchPattern(final String text, final String pattern) {
        final var stateTransitionTable = computeStateTransitionTable(pattern);
        FiniteAutomata finiteAutomata = new FiniteAutomata(stateTransitionTable);

        Set<Integer> indexFound = new TreeSet<>();
        for (int i = 0; i < text.length(); i++) {
            finiteAutomata.consume(text.charAt(i));

            if (finiteAutomata.getState() == pattern.length()) {
                indexFound.add(i - pattern.length() + 1);
            }
        }
        return indexFound;
    }

    /**
     * Computes the finite automata table for the given pattern.
     *
     * @param pattern The pattern to preprocess.
     * @return The state transition table.
     */
    private static int[][] computeStateTransitionTable(final String pattern) {
        final int patternLength = pattern.length();
        int[][] stateTransitionTable = new int[patternLength + 1][CHARS];

        for (int state = 0; state <= patternLength; ++state) {
            for (int x = 0; x < CHARS; ++x) {
                stateTransitionTable[state][x] = getNextState(pattern, patternLength, state, x);
            }
        }

        return stateTransitionTable;
    }

    /**
     * Gets the next state for the finite automata.
     *
     * @param pattern       The pattern being matched.
     * @param patternLength The length of the pattern.
     * @param state         The current state.
     * @param x             The current character from the input alphabet.
     * @return The next state.
     */
    private static int getNextState(final String pattern, final int patternLength, final int state, final int x) {
        // If the current state is less than the length of the pattern
        // and the character matches the pattern character, go to the next state
        if (state < patternLength && x == pattern.charAt(state)) {
            return state + 1;