-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAnagram.java
50 lines (42 loc) · 1.43 KB
/
Anagram.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package arraysandstrings;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Anagram
* https://github.com/careercup/CtCI-6th-Edition/tree/master/Java/Ch%2001.%20Arrays%20and%20Strings/Q1_02_Check_Permutation
* Difficulty : Easy
* Related Topics : Array, String
*
* created by Cenk Canarslan on 2021-10-26
*/
public class Anagram {
@Test
public void checkIsAnagram() {
Anagram anagram = new Anagram();
assertFalse(anagram.isAnagram("", ""));
assertFalse(anagram.isAnagram("", "aa"));
assertFalse(anagram.isAnagram("bb", ""));
assertFalse(anagram.isAnagram("c", "dd"));
assertTrue(anagram.isAnagram("integral", "triangle"));
assertFalse(anagram.isAnagram("integraL", "triangle"));
assertTrue(anagram.isAnagram("silent", "listen"));
assertFalse(anagram.isAnagram("Silent", "listen"));
}
private boolean isAnagram(String strA, String strB) {
if (strA.length() == 0 || strB.length() == 0 || strA.length() != strB.length()) {
return false;
}
int[] counter = new int[128];
for (int i = 0; i < strA.length(); i++) {
counter[strA.charAt(i)]++;
counter[strB.charAt(i)]--;
}
for (int j = 0; j < 128; j++) {
if (counter[j] != 0) {
return false;
}
}
return true;
}
}