Skip to content

Commit 8571c7a

Browse files
refactor: unified duplicate Anagram classes into a single implementation
1 parent dfcef2d commit 8571c7a

4 files changed

Lines changed: 29 additions & 198 deletions

File tree

src/main/java/com/thealgorithms/strings/Anagrams.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ private Anagrams() {
2323
* @param t the second string
2424
* @return true if the strings are anagrams, false otherwise
2525
*/
26-
public static boolean approach1(String s, String t) {
26+
public static boolean areAnagramsBySorting(String s, String t) {
27+
s = s.toLowerCase().replaceAll("[^a-z]", "");
28+
t = t.toLowerCase().replaceAll("[^a-z]", "");
2729
if (s.length() != t.length()) {
2830
return false;
2931
}
@@ -43,17 +45,18 @@ public static boolean approach1(String s, String t) {
4345
* @param t the second string
4446
* @return true if the strings are anagrams, false otherwise
4547
*/
46-
public static boolean approach2(String s, String t) {
47-
if (s.length() != t.length()) {
48-
return false;
48+
public static boolean areAnagramsByCountingChars(String s, String t) {
49+
s = s.toLowerCase().replaceAll("[^a-z]", "");
50+
t = t.toLowerCase().replaceAll("[^a-z]", "");
51+
int[] dict = new int[128];
52+
for (char ch : s.toCharArray()) {
53+
dict[ch]++;
4954
}
50-
int[] charCount = new int[26];
51-
for (int i = 0; i < s.length(); i++) {
52-
charCount[s.charAt(i) - 'a']++;
53-
charCount[t.charAt(i) - 'a']--;
55+
for (char ch : t.toCharArray()) {
56+
dict[ch]--;
5457
}
55-
for (int count : charCount) {
56-
if (count != 0) {
58+
for (int e : dict) {
59+
if (e != 0) {
5760
return false;
5861
}
5962
}
@@ -70,7 +73,9 @@ public static boolean approach2(String s, String t) {
7073
* @param t the second string
7174
* @return true if the strings are anagrams, false otherwise
7275
*/
73-
public static boolean approach3(String s, String t) {
76+
public static boolean areAnagramsByCountingCharsSingleArray(String s, String t) {
77+
s = s.toLowerCase().replaceAll("[^a-z]", "");
78+
t = t.toLowerCase().replaceAll("[^a-z]", "");
7479
if (s.length() != t.length()) {
7580
return false;
7681
}
@@ -96,7 +101,9 @@ public static boolean approach3(String s, String t) {
96101
* @param t the second string
97102
* @return true if the strings are anagrams, false otherwise
98103
*/
99-
public static boolean approach4(String s, String t) {
104+
public static boolean areAnagramsUsingHashMap(String s, String t) {
105+
s = s.toLowerCase().replaceAll("[^a-z]", "");
106+
t = t.toLowerCase().replaceAll("[^a-z]", "");
100107
if (s.length() != t.length()) {
101108
return false;
102109
}
@@ -123,7 +130,9 @@ public static boolean approach4(String s, String t) {
123130
* @param t the second string
124131
* @return true if the strings are anagrams, false otherwise
125132
*/
126-
public static boolean approach5(String s, String t) {
133+
public static boolean areAnagramsBySingleFreqArray(String s, String t) {
134+
s = s.toLowerCase().replaceAll("[^a-z]", "");
135+
t = t.toLowerCase().replaceAll("[^a-z]", "");
127136
if (s.length() != t.length()) {
128137
return false;
129138
}

src/main/java/com/thealgorithms/strings/CheckAnagrams.java

Lines changed: 0 additions & 110 deletions
This file was deleted.

src/test/java/com/thealgorithms/strings/AnagramsTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,37 @@ record AnagramTestCase(String input1, String input2, boolean expected) {
1313

1414
private static Stream<AnagramTestCase> anagramTestData() {
1515
return Stream.of(new AnagramTestCase("late", "tale", true), new AnagramTestCase("late", "teal", true), new AnagramTestCase("listen", "silent", true), new AnagramTestCase("hello", "olelh", true), new AnagramTestCase("hello", "world", false), new AnagramTestCase("deal", "lead", true),
16-
new AnagramTestCase("binary", "brainy", true), new AnagramTestCase("adobe", "abode", true), new AnagramTestCase("cat", "act", true), new AnagramTestCase("cat", "cut", false));
16+
new AnagramTestCase("binary", "brainy", true), new AnagramTestCase("adobe", "abode", true), new AnagramTestCase("cat", "act", true), new AnagramTestCase("cat", "cut", false), new AnagramTestCase("Listen", "Silent", true), new AnagramTestCase("Dormitory", "DirtyRoom", true),
17+
new AnagramTestCase("Schoolmaster", "TheClassroom", true), new AnagramTestCase("Astronomer", "MoonStarer", true), new AnagramTestCase("Conversation", "VoicesRantOn", true));
1718
}
1819

1920
@ParameterizedTest
2021
@MethodSource("anagramTestData")
2122
void testApproach1(AnagramTestCase testCase) {
22-
assertEquals(testCase.expected(), Anagrams.approach1(testCase.input1(), testCase.input2()));
23+
assertEquals(testCase.expected(), Anagrams.areAnagramsBySorting(testCase.input1(), testCase.input2()));
2324
}
2425

2526
@ParameterizedTest
2627
@MethodSource("anagramTestData")
2728
void testApproach2(AnagramTestCase testCase) {
28-
assertEquals(testCase.expected(), Anagrams.approach2(testCase.input1(), testCase.input2()));
29+
assertEquals(testCase.expected(), Anagrams.areAnagramsByCountingChars(testCase.input1(), testCase.input2()));
2930
}
3031

3132
@ParameterizedTest
3233
@MethodSource("anagramTestData")
3334
void testApproach3(AnagramTestCase testCase) {
34-
assertEquals(testCase.expected(), Anagrams.approach3(testCase.input1(), testCase.input2()));
35+
assertEquals(testCase.expected(), Anagrams.areAnagramsByCountingCharsSingleArray(testCase.input1(), testCase.input2()));
3536
}
3637

3738
@ParameterizedTest
3839
@MethodSource("anagramTestData")
3940
void testApproach4(AnagramTestCase testCase) {
40-
assertEquals(testCase.expected(), Anagrams.approach4(testCase.input1(), testCase.input2()));
41+
assertEquals(testCase.expected(), Anagrams.areAnagramsUsingHashMap(testCase.input1(), testCase.input2()));
4142
}
4243

4344
@ParameterizedTest
4445
@MethodSource("anagramTestData")
4546
void testApproach5(AnagramTestCase testCase) {
46-
assertEquals(testCase.expected(), Anagrams.approach5(testCase.input1(), testCase.input2()));
47+
assertEquals(testCase.expected(), Anagrams.areAnagramsBySingleFreqArray(testCase.input1(), testCase.input2()));
4748
}
4849
}

src/test/java/com/thealgorithms/strings/CheckAnagramsTest.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)