refactor: unified duplicate Anagram classes into a single implementation (#6290)

This commit is contained in:
Deniz Altunkapan
2025-06-11 19:04:06 +02:00
committed by GitHub
parent 0b21bb0a38
commit 1745d19f09
9 changed files with 38 additions and 212 deletions

View File

@@ -1,3 +1,7 @@
package com.thealgorithms.dynamicprogramming;
import java.util.Arrays;
/**
* Author: Siddhant Swarup Mallick
* Github: https://github.com/siddhant2002
@@ -12,11 +16,6 @@
* This program calculates the number of unique paths possible for a robot to reach the bottom-right corner
* of an m x n grid using dynamic programming.
*/
package com.thealgorithms.dynamicprogramming;
import java.util.Arrays;
public final class UniquePaths {
private UniquePaths() {

View File

@@ -1,3 +1,5 @@
package com.thealgorithms.dynamicprogramming;
/**
*
* Author: Janmesh Singh
@@ -11,9 +13,6 @@
* Use DP to return True if the pattern matches the entire text and False otherwise
*
*/
package com.thealgorithms.dynamicprogramming;
public final class WildcardMatching {
private WildcardMatching() {
}

View File

@@ -1,11 +1,11 @@
package com.thealgorithms.maths;
/**
* A number is said to be Dudeney if the sum of the digits, is the cube root of the entered number.
* Example- Let the number be 512, its sum of digits is 5+1+2=8. The cube root of 512 is also 8.
* Since, the sum of the digits is equal to the cube root of the entered number;
* it is a Dudeney Number.
*/
package com.thealgorithms.maths;
public final class DudeneyNumber {
private DudeneyNumber() {
}

View File

@@ -1,7 +1,3 @@
/**
* @author Md Asif Joardar
*/
package com.thealgorithms.scheduling;
import com.thealgorithms.devutils.entities.ProcessDetails;
@@ -11,6 +7,7 @@ import java.util.List;
import java.util.Queue;
/**
* @author Md Asif Joardar
* The Round-robin scheduling algorithm is a kind of preemptive First come, First Serve CPU
* Scheduling algorithm. This can be understood here -
* https://www.scaler.com/topics/round-robin-scheduling-in-os/

View File

@@ -23,7 +23,9 @@ public final class Anagrams {
* @param t the second string
* @return true if the strings are anagrams, false otherwise
*/
public static boolean approach1(String s, String t) {
public static boolean areAnagramsBySorting(String s, String t) {
s = s.toLowerCase().replaceAll("[^a-z]", "");
t = t.toLowerCase().replaceAll("[^a-z]", "");
if (s.length() != t.length()) {
return false;
}
@@ -43,17 +45,18 @@ public final class Anagrams {
* @param t the second string
* @return true if the strings are anagrams, false otherwise
*/
public static boolean approach2(String s, String t) {
if (s.length() != t.length()) {
return false;
public static boolean areAnagramsByCountingChars(String s, String t) {
s = s.toLowerCase().replaceAll("[^a-z]", "");
t = t.toLowerCase().replaceAll("[^a-z]", "");
int[] dict = new int[128];
for (char ch : s.toCharArray()) {
dict[ch]++;
}
int[] charCount = new int[26];
for (int i = 0; i < s.length(); i++) {
charCount[s.charAt(i) - 'a']++;
charCount[t.charAt(i) - 'a']--;
for (char ch : t.toCharArray()) {
dict[ch]--;
}
for (int count : charCount) {
if (count != 0) {
for (int e : dict) {
if (e != 0) {
return false;
}
}
@@ -70,7 +73,9 @@ public final class Anagrams {
* @param t the second string
* @return true if the strings are anagrams, false otherwise
*/
public static boolean approach3(String s, String t) {
public static boolean areAnagramsByCountingCharsSingleArray(String s, String t) {
s = s.toLowerCase().replaceAll("[^a-z]", "");
t = t.toLowerCase().replaceAll("[^a-z]", "");
if (s.length() != t.length()) {
return false;
}
@@ -96,7 +101,9 @@ public final class Anagrams {
* @param t the second string
* @return true if the strings are anagrams, false otherwise
*/
public static boolean approach4(String s, String t) {
public static boolean areAnagramsUsingHashMap(String s, String t) {
s = s.toLowerCase().replaceAll("[^a-z]", "");
t = t.toLowerCase().replaceAll("[^a-z]", "");
if (s.length() != t.length()) {
return false;
}
@@ -123,7 +130,9 @@ public final class Anagrams {
* @param t the second string
* @return true if the strings are anagrams, false otherwise
*/
public static boolean approach5(String s, String t) {
public static boolean areAnagramsBySingleFreqArray(String s, String t) {
s = s.toLowerCase().replaceAll("[^a-z]", "");
t = t.toLowerCase().replaceAll("[^a-z]", "");
if (s.length() != t.length()) {
return false;
}

View File

@@ -1,110 +0,0 @@
package com.thealgorithms.strings;
import java.util.HashMap;
import java.util.Map;
/**
* Two strings are anagrams if they are made of the same letters arranged
* differently (ignoring the case).
*/
public final class CheckAnagrams {
private CheckAnagrams() {
}
/**
* Check if two strings are anagrams or not
*
* @param s1 the first string
* @param s2 the second string
* @return {@code true} if two string are anagrams, otherwise {@code false}
*/
public static boolean isAnagrams(String s1, String s2) {
int l1 = s1.length();
int l2 = s2.length();
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
Map<Character, Integer> charAppearances = new HashMap<>();
for (int i = 0; i < l1; i++) {
char c = s1.charAt(i);
int numOfAppearances = charAppearances.getOrDefault(c, 0);
charAppearances.put(c, numOfAppearances + 1);
}
for (int i = 0; i < l2; i++) {
char c = s2.charAt(i);
if (!charAppearances.containsKey(c)) {
return false;
}
charAppearances.put(c, charAppearances.get(c) - 1);
}
for (int cnt : charAppearances.values()) {
if (cnt != 0) {
return false;
}
}
return true;
}
/**
* If given strings contain Unicode symbols.
* The first 128 ASCII codes are identical to Unicode.
* This algorithm is case-sensitive.
*
* @param s1 the first string
* @param s2 the second string
* @return true if two string are anagrams, otherwise false
*/
public static boolean isAnagramsUnicode(String s1, String s2) {
int[] dict = new int[128];
for (char ch : s1.toCharArray()) {
dict[ch]++;
}
for (char ch : s2.toCharArray()) {
dict[ch]--;
}
for (int e : dict) {
if (e != 0) {
return false;
}
}
return true;
}
/**
* If given strings contain only lowercase English letters.
* <p>
* The main "trick":
* To map each character from the first string 's1' we need to subtract an integer value of 'a' character
* as 'dict' array starts with 'a' character.
*
* @param s1 the first string
* @param s2 the second string
* @return true if two string are anagrams, otherwise false
*/
public static boolean isAnagramsOptimised(String s1, String s2) {
// 26 - English alphabet length
int[] dict = new int[26];
for (char ch : s1.toCharArray()) {
checkLetter(ch);
dict[ch - 'a']++;
}
for (char ch : s2.toCharArray()) {
checkLetter(ch);
dict[ch - 'a']--;
}
for (int e : dict) {
if (e != 0) {
return false;
}
}
return true;
}
private static void checkLetter(char ch) {
int index = ch - 'a';
if (index < 0 || index >= 26) {
throw new IllegalArgumentException("Strings must contain only lowercase English letters!");
}
}
}