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();
}
}