refactor: clean up duplicate algorithm implementations to reduce maintenance overhead (#7256)

refactor: remove duplicate algorithm implementations

Removed the following duplicate implementations:
- searches/PerfectBinarySearch.java (duplicate of IterativeBinarySearch)
- searches/SortOrderAgnosticBinarySearch.java (duplicate of OrderAgnosticBinarySearch)
- strings/LongestPalindromicSubstring.java (duplicate of dynamicprogramming version)
- strings/ValidParentheses.java (duplicate of stacks version)
- others/cn/HammingDistance.java (duplicate - strings version handles text)
- others/NewManShanksPrimeTest.java (orphan test in wrong package)

Updated DIRECTORY.md to reflect the changes.

Fixes #7253

Co-authored-by: Ahmed Allam <60698204+AllamF5J@users.noreply.github.com>
This commit is contained in:
Ahmed Allam
2026-02-03 22:32:37 +02:00
committed by GitHub
parent c6703d337e
commit 8e30bcbb02
12 changed files with 0 additions and 474 deletions

View File

@@ -1,32 +0,0 @@
package com.thealgorithms.others.cn;
public final class HammingDistance {
private HammingDistance() {
}
private static void checkChar(char inChar) {
if (inChar != '0' && inChar != '1') {
throw new IllegalArgumentException("Input must be a binary string.");
}
}
public static int compute(char charA, char charB) {
checkChar(charA);
checkChar(charB);
return charA == charB ? 0 : 1;
}
public static int compute(String bitsStrA, String bitsStrB) {
if (bitsStrA.length() != bitsStrB.length()) {
throw new IllegalArgumentException("Input strings must have the same length.");
}
int totalErrorBitCount = 0;
for (int i = 0; i < bitsStrA.length(); i++) {
totalErrorBitCount += compute(bitsStrA.charAt(i), bitsStrB.charAt(i));
}
return totalErrorBitCount;
}
}

View File

@@ -1,54 +0,0 @@
package com.thealgorithms.searches;
import com.thealgorithms.devutils.searches.SearchAlgorithm;
/**
* Binary search is one of the most popular algorithms The algorithm finds the
* position of a target value within a sorted array
*
* <p>
* Worst-case performance O(log n) Best-case performance O(1) Average
* performance O(log n) Worst-case space complexity O(1)
*
* @author D Sunil (https://github.com/sunilnitdgp)
* @see SearchAlgorithm
*/
public class PerfectBinarySearch<T> implements SearchAlgorithm {
/**
* @param array is an array where the element should be found
* @param key is an element which should be found
* @param <T> is any comparable type
* @return index of the element
*/
@Override
public <T extends Comparable<T>> int find(T[] array, T key) {
return search(array, key, 0, array.length - 1);
}
/**
* This method implements the Generic Binary Search iteratively.
*
* @param array The array to make the binary search
* @param key The number you are looking for
* @return the location of the key, or -1 if not found
*/
private static <T extends Comparable<T>> int search(T[] array, T key, int left, int right) {
while (left <= right) {
int median = (left + right) >>> 1;
int comp = key.compareTo(array[median]);
if (comp == 0) {
return median; // Key found
}
if (comp < 0) {
right = median - 1; // Adjust the right bound
} else {
left = median + 1; // Adjust the left bound
}
}
return -1; // Key not found
}
}

View File

@@ -1,30 +0,0 @@
package com.thealgorithms.searches;
public final class SortOrderAgnosticBinarySearch {
private SortOrderAgnosticBinarySearch() {
}
public static int find(int[] arr, int key) {
int start = 0;
int end = arr.length - 1;
boolean arrDescending = arr[start] > arr[end]; // checking for Array is in ascending order or descending order.
while (start <= end) {
int mid = end - start / 2;
if (arr[mid] == key) {
return mid;
}
if (arrDescending) { // boolean is true then our array is in descending order
if (key < arr[mid]) {
start = mid + 1;
} else {
end = mid - 1;
}
} else { // otherwise our array is in ascending order
if (key > arr[mid]) {
start = mid + 1;
} else {
end = mid - 1;
}
}
}
return -1;
}
}

View File

@@ -1,37 +0,0 @@
package com.thealgorithms.strings;
final class LongestPalindromicSubstring {
private LongestPalindromicSubstring() {
}
/**
* Finds the longest palindromic substring in the given string.
*
* @param s the input string
* @return the longest palindromic substring
*/
public static String longestPalindrome(String s) {
if (s == null || s.isEmpty()) {
return "";
}
String maxStr = "";
for (int i = 0; i < s.length(); ++i) {
for (int j = i; j < s.length(); ++j) {
if (isValid(s, i, j) && (j - i + 1 > maxStr.length())) {
maxStr = s.substring(i, j + 1);
}
}
}
return maxStr;
}
private static boolean isValid(String s, int lo, int hi) {
int n = hi - lo + 1;
for (int i = 0; i < n / 2; ++i) {
if (s.charAt(lo + i) != s.charAt(hi - i)) {
return false;
}
}
return true;
}
}

View File

@@ -1,53 +0,0 @@
package com.thealgorithms.strings;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Map;
/**
* Validates if a given string has valid matching parentheses.
* <p>
* A string is considered valid if:
* <ul>
* <li>Open brackets are closed by the same type of brackets.</li>
* <li>Brackets are closed in the correct order.</li>
* <li>Every closing bracket has a corresponding open bracket of the same type.</li>
* </ul>
*
* Allowed characters: '(', ')', '{', '}', '[', ']'
*/
public final class ValidParentheses {
private ValidParentheses() {
}
private static final Map<Character, Character> BRACKET_PAIRS = Map.of(')', '(', '}', '{', ']', '[');
/**
* Checks if the input string has valid parentheses.
*
* @param s the string containing only bracket characters
* @return true if valid, false otherwise
* @throws IllegalArgumentException if the string contains invalid characters or is null
*/
public static boolean isValid(String s) {
if (s == null) {
throw new IllegalArgumentException("Input string cannot be null");
}
Deque<Character> stack = new ArrayDeque<>();
for (char c : s.toCharArray()) {
if (BRACKET_PAIRS.containsValue(c)) {
stack.push(c); // opening bracket
} else if (BRACKET_PAIRS.containsKey(c)) {
if (stack.isEmpty() || stack.pop() != BRACKET_PAIRS.get(c)) {
return false;
}
} else {
throw new IllegalArgumentException("Unexpected character: " + c);
}
}
return stack.isEmpty();
}
}

View File

@@ -1,49 +0,0 @@
package com.thealgorithms.others;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.thealgorithms.dynamicprogramming.NewManShanksPrime;
import org.junit.jupiter.api.Test;
public class NewManShanksPrimeTest {
@Test
void testOne() {
assertTrue(NewManShanksPrime.nthManShanksPrime(1, 1));
}
@Test
void testTwo() {
assertTrue(NewManShanksPrime.nthManShanksPrime(2, 3));
}
@Test
void testThree() {
assertTrue(NewManShanksPrime.nthManShanksPrime(3, 7));
}
@Test
void testFour() {
assertTrue(NewManShanksPrime.nthManShanksPrime(4, 17));
}
@Test
void testFive() {
assertTrue(NewManShanksPrime.nthManShanksPrime(5, 41));
}
@Test
void testSix() {
assertTrue(NewManShanksPrime.nthManShanksPrime(6, 99));
}
@Test
void testSeven() {
assertTrue(NewManShanksPrime.nthManShanksPrime(7, 239));
}
@Test
void testEight() {
assertTrue(NewManShanksPrime.nthManShanksPrime(8, 577));
}
}

View File

@@ -1,82 +0,0 @@
package com.thealgorithms.others.cn;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
public class HammingDistanceTest {
@Test
public void checkForDifferentBits() {
int answer = HammingDistance.compute("000", "011");
Assertions.assertThat(answer).isEqualTo(2);
}
/*
1 0 1 0 1
1 1 1 1 0
----------
0 1 0 1 1
*/
@Test
public void checkForDifferentBitsLength() {
int answer = HammingDistance.compute("10101", "11110");
Assertions.assertThat(answer).isEqualTo(3);
}
@Test
public void checkForSameBits() {
String someBits = "111";
int answer = HammingDistance.compute(someBits, someBits);
Assertions.assertThat(answer).isEqualTo(0);
}
@Test
public void checkForLongDataBits() {
int answer = HammingDistance.compute("10010101101010000100110100", "00110100001011001100110101");
Assertions.assertThat(answer).isEqualTo(7);
}
@Test
public void mismatchDataBits() {
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { HammingDistance.compute("100010", "00011"); });
Assertions.assertThat(ex.getMessage()).contains("must have the same length");
}
@Test
public void mismatchDataBits2() {
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { HammingDistance.compute("1", "11"); });
Assertions.assertThat(ex.getMessage()).contains("must have the same length");
}
@Test
public void checkForLongDataBitsSame() {
String someBits = "10010101101010000100110100";
int answer = HammingDistance.compute(someBits, someBits);
Assertions.assertThat(answer).isEqualTo(0);
}
@Test
public void checkForEmptyInput() {
String someBits = "";
int answer = HammingDistance.compute(someBits, someBits);
Assertions.assertThat(answer).isEqualTo(0);
}
@Test
public void checkForInputOfLength1() {
String someBits = "0";
int answer = HammingDistance.compute(someBits, someBits);
Assertions.assertThat(answer).isEqualTo(0);
}
@Test
public void computeThrowsExceptionWhenInputsAreNotBitStrs() {
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { HammingDistance.compute("1A", "11"); });
Assertions.assertThat(ex.getMessage()).contains("must be a binary string");
}
}

View File

@@ -1,44 +0,0 @@
package com.thealgorithms.searches;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
/**
* @author D Sunil (https://github.com/sunilnitdgp)
* @see PerfectBinarySearch
*/
public class PerfectBinarySearchTest {
@Test
public void testIntegerBinarySearch() {
Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
PerfectBinarySearch<Integer> binarySearch = new PerfectBinarySearch<>();
// Test cases for elements present in the array
assertEquals(0, binarySearch.find(array, 1)); // First element
assertEquals(4, binarySearch.find(array, 5)); // Middle element
assertEquals(9, binarySearch.find(array, 10)); // Last element
assertEquals(6, binarySearch.find(array, 7)); // Element in the middle
// Test cases for elements not in the array
assertEquals(-1, binarySearch.find(array, 0)); // Element before the array
assertEquals(-1, binarySearch.find(array, 11)); // Element after the array
assertEquals(-1, binarySearch.find(array, 100)); // Element not in the array
}
@Test
public void testStringBinarySearch() {
String[] array = {"apple", "banana", "cherry", "date", "fig"};
PerfectBinarySearch<String> binarySearch = new PerfectBinarySearch<>();
// Test cases for elements not in the array
assertEquals(-1, binarySearch.find(array, "apricot")); // Element not in the array
assertEquals(-1, binarySearch.find(array, "bananaa")); // Element not in the array
// Test cases for elements present in the array
assertEquals(0, binarySearch.find(array, "apple")); // First element
assertEquals(2, binarySearch.find(array, "cherry")); // Middle element
assertEquals(4, binarySearch.find(array, "fig")); // Last element
}
}

View File

@@ -1,26 +0,0 @@
package com.thealgorithms.searches;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class SortOrderAgnosticBinarySearchTest {
@Test
public void testAscending() {
int[] arr = {1, 2, 3, 4, 5}; // for ascending order.
int target = 2;
int ans = SortOrderAgnosticBinarySearch.find(arr, target);
int excepted = 1;
assertEquals(excepted, ans);
}
@Test
public void testDescending() {
int[] arr = {5, 4, 3, 2, 1}; // for descending order.
int target = 2;
int ans = SortOrderAgnosticBinarySearch.find(arr, target);
int excepted = 3;
assertEquals(excepted, ans);
}
}

View File

@@ -1,21 +0,0 @@
package com.thealgorithms.strings;
import static org.junit.jupiter.api.Assertions.assertEquals;
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 LongestPalindromicSubstringTest {
@ParameterizedTest
@MethodSource("provideTestCasesForLongestPalindrome")
void testLongestPalindrome(String input, String expected) {
assertEquals(expected, LongestPalindromicSubstring.longestPalindrome(input));
}
private static Stream<Arguments> provideTestCasesForLongestPalindrome() {
return Stream.of(Arguments.of("babad", "bab"), Arguments.of("cbbd", "bb"), Arguments.of("a", "a"), Arguments.of("", ""), Arguments.of("abc", "a"), Arguments.of(null, ""), Arguments.of("aaaaa", "aaaaa"));
}
}

View File

@@ -1,33 +0,0 @@
package com.thealgorithms.strings;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
public class ValidParenthesesTest {
@ParameterizedTest(name = "Input: \"{0}\" → Expected: {1}")
@CsvSource({"'()', true", "'()[]{}', true", "'(]', false", "'{[]}', true", "'([{}])', true", "'([)]', false", "'', true", "'(', false", "')', false", "'{{{{}}}}', true", "'[({})]', true", "'[(])', false", "'[', false", "']', false", "'()()()()', true", "'(()', false", "'())', false",
"'{[()()]()}', true"})
void
testIsValid(String input, boolean expected) {
assertEquals(expected, ValidParentheses.isValid(input));
}
@Test
void testNullInputThrows() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> ValidParentheses.isValid(null));
assertEquals("Input string cannot be null", ex.getMessage());
}
@ParameterizedTest(name = "Input: \"{0}\" → throws IllegalArgumentException")
@CsvSource({"'a'", "'()a'", "'[123]'", "'{hello}'", "'( )'", "'\t'", "'\n'", "'@#$%'"})
void testInvalidCharactersThrow(String input) {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> ValidParentheses.isValid(input));
assertTrue(ex.getMessage().startsWith("Unexpected character"));
}
}