ronantakizawa/codereview-bench huggingface dataset:
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java (Java)

Code before:
package com.thealgorithms.strings;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public final class LetterCombinationsOfPhoneNumber {

    private static final char EMPTY = '\0';

    // Mapping of numbers to corresponding letters on a phone keypad
    private static final String[] KEYPAD = new String[] {" ", EMPTY + "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

    private LetterCombinationsOfPhoneNumber() {
    }

    /**
     * Generates a list of all possible letter combinations that the provided
     * array of numbers could represent on a phone keypad.
     *
     * @param numbers an array of integers representing the phone numbers
     * @return a list of possible letter combinations
     */
    public static List<String> getCombinations(int[] numbers) {
        if (numbers == null) {
            return List.of("");
        }
        return generateCombinations(numbers, 0, new StringBuilder());
    }

    /**
     * Recursive method to generate combinations of letters from the phone keypad.
     *
     * @param numbers the input array of phone numbers
     * @param index   the current index in the numbers array being processed
     * @param current a StringBuilder holding the current combination of letters
     * @return a list of letter combinations formed from the given numbers

Review comment (suggestion):
```suggestion
    private static final String[] KEYPAD = new String[] {" ", String.valueOf(EMPTY), "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
```

Code after:
package com.thealgorithms.strings;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public final class LetterCombinationsOfPhoneNumber {

    private static final char EMPTY = '\0';

    // Mapping of numbers to corresponding letters on a phone keypad
    private static final String[] KEYPAD = new String[] {" ", String.valueOf(EMPTY), "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

    private LetterCombinationsOfPhoneNumber() {
    }

    /**
     * Generates a list of all possible letter combinations that the provided
     * array of numbers could represent on a phone keypad.
     *
     * @param numbers an array of integers representing the phone numbers
     * @return a list of possible letter combinations
     */
    public static List<String> getCombinations(int[] numbers) {
        if (numbers == null) {
            return List.of("");
        }
        return generateCombinations(numbers, 0, new StringBuilder());
    }

    /**
     * Recursive method to generate combinations of letters from the phone keypad.
     *
     * @param numbers the input array of phone numbers
     * @param index   the current index in the numbers array being processed
     * @param current a StringBuilder holding the current combination of letters
     * @return a list of letter combinations formed from the given numbers
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/others/StringMatchFiniteAutomata.java (Java)

Code before:
                for (int i = 0; i < ns - 1; i++) {
                    if (pattern.charAt(i) != pattern.charAt(state - ns + i + 1)) {
                        match = false;
                        break;
                    }
                }
                if (match) {
                    return ns;
                }
            }
        }

        // If no prefix which is also a suffix is found, return 0
        return 0;
    }

    /**
     * A class representing the finite automata for pattern matching.
     */
    private static final class FiniteAutomata {
        private int state;
        private final int[][] stateTransitionTable;

        private FiniteAutomata(int[][] stateTransitionTable) {
            this.stateTransitionTable = stateTransitionTable;
            this.state = 0; // Initial state
        }

        /**
         * Consumes an input character and transitions to the next state.
         *
         * @param input The input character.
         */
        private void consume(final char input) {
            state = stateTransitionTable[state][input];
        }

        /**
         * Gets the current state of the finite automata.
         *
         * @return The current state.
         */
        private int getState() {
            return state;
        }
    }
}


Review comment (suggestion):
```suggestion
        private int state = 0;
        private final int[][] stateTransitionTable;

        private FiniteAutomata(int[][] stateTransitionTable) {
            this.stateTransitionTable = stateTransitionTable;
```

Code after:
                for (int i = 0; i < ns - 1; i++) {
                    if (pattern.charAt(i) != pattern.charAt(state - ns + i + 1)) {
                        match = false;
                        break;
                    }
                }
                if (match) {
                    return ns;
                }
            }
        }

        // If no prefix which is also a suffix is found, return 0
        return 0;
    }

    /**
     * A class representing the finite automata for pattern matching.
     */
    private static final class FiniteAutomata {
        private int state = 0;
        private final int[][] stateTransitionTable;

        private FiniteAutomata(int[][] stateTransitionTable) {
            this.stateTransitionTable = stateTransitionTable;
        }

        /**
         * Consumes an input character and transitions to the next state.
         *
         * @param input The input character.
         */
        private void consume(final char input) {
            state = stateTransitionTable[state][input];
        }

        /**
         * Gets the current state of the finite automata.
         *
         * @return The current state.
         */
        private int getState() {
            return state;
        }
    }
}
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/others/StringMatchFiniteAutomataTest.java (Java)

Code before:
package com.thealgorithms.others;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Set;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class StringMatchFiniteAutomataTest {

    @ParameterizedTest
    @MethodSource("provideTestCases")
    void searchPattern(String text, String pattern, Set<Integer> expectedOutput) {
        assertEquals(expectedOutput, StringMatchFiniteAutomata.searchPattern(text, pattern));
    }

    private static Stream<Arguments> provideTestCases() {
        return Stream.of(Arguments.of("abcbcabc", "abc", Set.of(0, 5)), //
            Arguments.of("", "abc", Set.of()), //
            Arguments.of("abcdefg", "xyz", Set.of()), //
            Arguments.of("abcde", "", Set.of(1, 2, 3, 4, 5)), //
            Arguments.of("abcabcabc", "abc", Set.of(0, 3, 6)), //
            Arguments.of("abcabcabc", "abcabcabc", Set.of(0)), //
            Arguments.of("aaabbbaaa", "aaa", Set.of(0, 6)), //
            Arguments.of("abcdefg", "efg", Set.of(4)) //
        );
    }
}


Review comment (suggestion):
```suggestion
            Arguments.of("", "abc", Set.of()), //
            Arguments.of("", "", Set.of()), //
            Arguments.of("a", "b", Set.of()), //
            Arguments.of("abc", "bcd", Set.of()), //
```
And please remove the `//` at the end of each line.

Code after:
package com.thealgorithms.others;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Set;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class StringMatchFiniteAutomataTest {

    @ParameterizedTest
    @MethodSource("provideTestCases")
    void searchPattern(String text, String pattern, Set<Integer> expectedOutput) {
        assertEquals(expectedOutput, StringMatchFiniteAutomata.searchPattern(text, pattern));
    }

    private static Stream<Arguments> provideTestCases() {
        return Stream.of(Arguments.of("abcbcabc", "abc", Set.of(0, 5)), Arguments.of("", "abc", Set.of()), Arguments.of("", "", Set.of()), Arguments.of("a", "b", Set.of()), Arguments.of("a", "a", Set.of(0)), Arguments.of("abcdabcabcabcd", "abcd", Set.of(0, 10)), Arguments.of("abc", "bcd", Set.of()),
            Arguments.of("abcdefg", "xyz", Set.of()), Arguments.of("abcde", "", Set.of(1, 2, 3, 4, 5)), Arguments.of("abcabcabc", "abc", Set.of(0, 3, 6)), Arguments.of("abcabcabc", "abcabcabc", Set.of(0)), Arguments.of("aaabbbaaa", "aaa", Set.of(0, 6)), Arguments.of("abcdefg", "efg", Set.of(4)));
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/others/StringMatchFiniteAutomata.java (Java)

Code before:
package com.thealgorithms.others;

import java.util.Set;
import java.util.TreeSet;

/**
 * A class to perform string matching using <a href="https://en.wikipedia.org/wiki/Finite-state_machine">finite automata</a>.
 *
 * @author <a href="https://github.com/prateekKrOraon">Prateek Kumar Oraon</a>
 */
public final class StringMatchFiniteAutomata {

    // Constants
    private static final int CHARS = 256; // Total number of characters in the input alphabet

    // Private constructor to prevent instantiation
    private StringMatchFiniteAutomata() {
    }

    /**
     * Searches for the pattern in the given text using finite automata.
     *
     * @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;
    }

Review comment (suggestion):
```suggestion
    private static final int CHARS = Character.MAX_VALUE + 1; // Total number of characters in the input alphabet
```
`char` has 16 bits in java. Therefore maybe one could consider using a `Map`-like structure to represent the state transition table.

Code after:
package com.thealgorithms.others;

import java.util.Set;
import java.util.TreeSet;

/**
 * A class to perform string matching using <a href="https://en.wikipedia.org/wiki/Finite-state_machine">finite automata</a>.
 *
 * @author <a href="https://github.com/prateekKrOraon">Prateek Kumar Oraon</a>
 */
public final class StringMatchFiniteAutomata {

    // Constants
    private static final int CHARS = Character.MAX_VALUE + 1; // Total number of characters in the input alphabet

    // Private constructor to prevent instantiation
    private StringMatchFiniteAutomata() {
    }

    /**
     * Searches for the pattern in the given text using finite automata.
     *
     * @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;
    }
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/others/StringMatchFiniteAutomataTest.java (Java)

Code before:
package com.thealgorithms.others;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Set;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class StringMatchFiniteAutomataTest {

    @ParameterizedTest
    @MethodSource("provideTestCases")
    void searchPattern(String text, String pattern, Set<Integer> expectedOutput) {
        assertEquals(expectedOutput, StringMatchFiniteAutomata.searchPattern(text, pattern));
    }

    private static Stream<Arguments> provideTestCases() {
        return Stream.of(Arguments.of("abcbcabc", "abc", Set.of(0, 5)), Arguments.of("", "abc", Set.of()), Arguments.of("abcdefg", "xyz", Set.of()), Arguments.of("abcde", "", Set.of(1, 2, 3, 4, 5)), Arguments.of("abcabcabc", "abc", Set.of(0, 3, 6)), Arguments.of("abcabcabc", "abcabcabc", Set.of(0)),
            Arguments.of("aaabbbaaa", "aaa", Set.of(0, 6)), Arguments.of("abcdefg", "efg", Set.of(4)));
    }
}


Review comment (suggestion):
```suggestion
        return Stream.of(Arguments.of("abcbcabc", "abc", Set.of(0, 5)), Arguments.of("", "abc", Set.of()), Arguments.of("", "", Set.of()), Arguments.of("a", "b", Set.of()), Arguments.of("a", "a", Set.of(0)), Arguments.of("abcdabcabcabcd", "abcd", Set.of(0, 10)), Arguments.of("abc", "bcd", Set.of()),
            Arguments.of("abcdefg", "xyz", Set.of()), Arguments.of("abcde", "", Set.of(1, 2, 3, 4, 5)), Arguments.of("abcabcabc", "abc", Set.of(0, 3, 6)), Arguments.of("abcabcabc", "abcabcabc", Set.of(0)), Arguments.of("aaabbbaaa", "aaa", Set.of(0, 6)), Arguments.of("abcdefg", "efg", Set.of(4)));
```

Code after:
package com.thealgorithms.others;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Set;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class StringMatchFiniteAutomataTest {

    @ParameterizedTest
    @MethodSource("provideTestCases")
    void searchPattern(String text, String pattern, Set<Integer> expectedOutput) {
        assertEquals(expectedOutput, StringMatchFiniteAutomata.searchPattern(text, pattern));
    }

    private static Stream<Arguments> provideTestCases() {
        return Stream.of(Arguments.of("abcbcabc", "abc", Set.of(0, 5)), Arguments.of("", "abc", Set.of()), Arguments.of("", "", Set.of()), Arguments.of("a", "b", Set.of()), Arguments.of("a", "a", Set.of(0)), Arguments.of("abcdabcabcabcd", "abcd", Set.of(0, 10)), Arguments.of("abc", "bcd", Set.of()),
            Arguments.of("abcdefg", "xyz", Set.of()), Arguments.of("abcde", "", Set.of(1, 2, 3, 4, 5)), Arguments.of("abcabcabc", "abc", Set.of(0, 3, 6)), Arguments.of("abcabcabc", "abcabcabc", Set.of(0)), Arguments.of("aaabbbaaa", "aaa", Set.of(0, 6)), Arguments.of("abcdefg", "efg", Set.of(4)));
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java (Java)

Code before:
package com.thealgorithms.sorts;

/**
 * BinaryInsertionSort class implements the SortAlgorithm interface using the binary insertion sort technique.
 * Binary Insertion Sort improves upon the simple insertion sort by using binary search to find the appropriate
 * location to insert the new element, reducing the number of comparisons in the insertion step.
 */
public class BinaryInsertionSort implements SortAlgorithm {

    /**
     * Sorts the given array using the Binary Insertion Sort algorithm.
     *
     * @param <T> the type of elements in the array, which must implement the Comparable interface
     * @param array the array to be sorted
     * @return the sorted array
     */
    public <T extends Comparable<T>> T[] sort(T[] array) {
        for (int i = 1; i < array.length; i++) {
            T temp = array[i];
            int low = 0;
            int high = i - 1;

            while (low <= high) {
                final int mid = (low + high) >>> 1;
                if (temp.compareTo(array[mid]) < 0) {
                    high = mid - 1;
                } else {
                    low = mid + 1;
                }
            }

            for (int j = i; j >= low + 1; j--) {
                array[j] = array[j - 1];
            }

            array[low] = temp;
        }
        return array;
    }
}


Review comment (suggestion):
Since you are changing this line anyway, why not to:
```suggestion
            final T temp = array[i];
```

Code after:
package com.thealgorithms.sorts;

/**
 * BinaryInsertionSort class implements the SortAlgorithm interface using the binary insertion sort technique.
 * Binary Insertion Sort improves upon the simple insertion sort by using binary search to find the appropriate
 * location to insert the new element, reducing the number of comparisons in the insertion step.
 */
public class BinaryInsertionSort implements SortAlgorithm {

    /**
     * Sorts the given array using the Binary Insertion Sort algorithm.
     *
     * @param <T> the type of elements in the array, which must implement the Comparable interface
     * @param array the array to be sorted
     * @return the sorted array
     */
    public <T extends Comparable<T>> T[] sort(T[] array) {
        for (int i = 1; i < array.length; i++) {
            final T temp = array[i];
            int low = 0;
            int high = i - 1;

            while (low <= high) {
                final int mid = (low + high) >>> 1;
                if (temp.compareTo(array[mid]) < 0) {
                    high = mid - 1;
                } else {
                    low = mid + 1;
                }
            }

            for (int j = i; j >= low + 1; j--) {
                array[j] = array[j - 1];
            }

            array[low] = temp;
        }
        return array;
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/sorts/OddEvenSort.java (Java)

Code before:
                    SortUtils.swap(arr, i, i + 1);
                    sorted = false;
                }
            }
        }
        return arr;
    }

    public static void main(String[] args) {
        final Integer[] arr = new Integer[100];
        final Random random = new Random();

        // Print out unsorted elements
        for (int i = 0; i < arr.length; ++i) {
            arr[i] = random.nextInt(100) - 50;
            System.out.println(arr[i]);
        }
        System.out.println("--------------");
        final OddEvenSort oddEvenSort = new OddEvenSort();
        oddEvenSort.sort(arr);

        // Print Sorted elements
        for (int i = 0; i < arr.length - 1; ++i) {
            System.out.println(arr[i]);
            assert arr[i] <= arr[i + 1];
        }
    }
}


Review comment (suggestion):
The `main` method is completely redundant here.

```suggestion
```

Code after:
package com.thealgorithms.sorts;

/**
 * OddEvenSort class implements the SortAlgorithm interface using the odd-even sort technique.
 * Odd-even sort is a comparison sort related to bubble sort.
 * It operates by comparing all (odd, even)-indexed pairs of adjacent elements in the list and, if a pair is in the wrong order, swapping them.
 * The next step repeats this process for (even, odd)-indexed pairs. This process continues until the list is sorted.
 *
 */
public final class OddEvenSort implements SortAlgorithm {

    /**
     * Sorts the given array using the Odd-Even Sort algorithm.
     *
     * @param <T> the type of elements in the array, which must implement the Comparable interface
     * @param arr the array to be sorted
     * @return the sorted array
     */
    @Override
    public <T extends Comparable<T>> T[] sort(T[] arr) {
        boolean sorted = false;
        while (!sorted) {
            sorted = true;

            for (int i = 1; i < arr.length - 1; i += 2) {
                if (arr[i].compareTo(arr[i + 1]) > 0) {
                    SortUtils.swap(arr, i, i + 1);
                    sorted = false;
                }
            }

            for (int i = 0; i < arr.length - 1; i += 2) {
                if (arr[i].compareTo(arr[i + 1]) > 0) {
                    SortUtils.swap(arr, i, i + 1);
                    sorted = false;
                }
            }
        }
        return arr;
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/sorts/OddEvenSort.java (Java)

Code before:
package com.thealgorithms.sorts;

import java.util.Random;

/**
 * OddEvenSort class implements the SortAlgorithm interface using the odd-even sort technique.
 * Odd-even sort is a comparison sort related to bubble sort.
 * It operates by comparing all (odd, even)-indexed pairs of adjacent elements in the list and, if a pair is in the wrong order, swapping them.
 * The next step repeats this process for (even, odd)-indexed pairs. This process continues until the list is sorted.
 *
 */
public final class OddEvenSort implements SortAlgorithm {

    /**
     * Sorts the given array using the Odd-Even Sort algorithm.
     *
     * @param <T> the type of elements in the array, which must implement the Comparable interface
     * @param arr the array to be sorted
     * @return the sorted array
     */
    @Override
    public <T extends Comparable<T>> T[] sort(T[] arr) {
        boolean sorted = false;
        while (!sorted) {
            sorted = true;

            for (int i = 1; i < arr.length - 1; i += 2) {
                if (arr[i].compareTo(arr[i + 1]) > 0) {

Review comment (suggestion):
This is not needed, when `main` is removed.

```suggestion
```

Code after:
package com.thealgorithms.sorts;

/**
 * OddEvenSort class implements the SortAlgorithm interface using the odd-even sort technique.
 * Odd-even sort is a comparison sort related to bubble sort.
 * It operates by comparing all (odd, even)-indexed pairs of adjacent elements in the list and, if a pair is in the wrong order, swapping them.
 * The next step repeats this process for (even, odd)-indexed pairs. This process continues until the list is sorted.
 *
 */
public final class OddEvenSort implements SortAlgorithm {

    /**
     * Sorts the given array using the Odd-Even Sort algorithm.
     *
     * @param <T> the type of elements in the array, which must implement the Comparable interface
     * @param arr the array to be sorted
     * @return the sorted array
     */
    @Override
    public <T extends Comparable<T>> T[] sort(T[] arr) {
        boolean sorted = false;
        while (!sorted) {
            sorted = true;

            for (int i = 1; i < arr.length - 1; i += 2) {
                if (arr[i].compareTo(arr[i + 1]) > 0) {
                    SortUtils.swap(arr, i, i + 1);
                    sorted = false;