Skip to content

Commit 1745d19

Browse files
refactor: unified duplicate Anagram classes into a single implementation (#6290)
1 parent 0b21bb0 commit 1745d19

File tree

9 files changed

+38
-212
lines changed

9 files changed

+38
-212
lines changed

src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import java.util.Arrays;
4+
15
/**
26
* Author: Siddhant Swarup Mallick
37
* Github: https://github.com/siddhant2002
@@ -12,11 +16,6 @@
1216
* This program calculates the number of unique paths possible for a robot to reach the bottom-right corner
1317
* of an m x n grid using dynamic programming.
1418
*/
15-
16-
package com.thealgorithms.dynamicprogramming;
17-
18-
import java.util.Arrays;
19-
2019
public final class UniquePaths {
2120

2221
private UniquePaths() {

src/main/java/com/thealgorithms/dynamicprogramming/WildcardMatching.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
13
/**
24
*
35
* Author: Janmesh Singh
@@ -11,9 +13,6 @@
1113
* Use DP to return True if the pattern matches the entire text and False otherwise
1214
*
1315
*/
14-
15-
package com.thealgorithms.dynamicprogramming;
16-
1716
public final class WildcardMatching {
1817
private WildcardMatching() {
1918
}

src/main/java/com/thealgorithms/maths/DudeneyNumber.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
package com.thealgorithms.maths;
2+
13
/**
24
* A number is said to be Dudeney if the sum of the digits, is the cube root of the entered number.
35
* Example- Let the number be 512, its sum of digits is 5+1+2=8. The cube root of 512 is also 8.
46
* Since, the sum of the digits is equal to the cube root of the entered number;
57
* it is a Dudeney Number.
68
*/
7-
package com.thealgorithms.maths;
8-
99
public final class DudeneyNumber {
1010
private DudeneyNumber() {
1111
}

src/main/java/com/thealgorithms/scheduling/RRScheduling.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/**
2-
* @author Md Asif Joardar
3-
*/
4-
51
package com.thealgorithms.scheduling;
62

73
import com.thealgorithms.devutils.entities.ProcessDetails;
@@ -11,6 +7,7 @@
117
import java.util.Queue;
128

139
/**
10+
* @author Md Asif Joardar
1411
* The Round-robin scheduling algorithm is a kind of preemptive First come, First Serve CPU
1512
* Scheduling algorithm. This can be understood here -
1613
* https://www.scaler.com/topics/round-robin-scheduling-in-os/

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)