-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathSolution.java
More file actions
27 lines (24 loc) · 775 Bytes
/
Solution.java
File metadata and controls
27 lines (24 loc) · 775 Bytes
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
static boolean isAnagram(String a, String b) {
// a -> 97
// z -> 122
// A -> 65
// Z -> 90
int[] cache = new int[26];
if (a.length() != b.length()) { return false; }
for (int i = 0; i < a.length(); i++) {
int index = (int) a.charAt(i);
if (65 <= index && index <= 90) { index += 32; }
index -= 97;
cache[index]++;
}
for (int i = 0; i < b.length(); i++) {
int index = (int) b.charAt(i);
if (65 <= index && index <= 90) { index += 32; }
index -= 97;
cache[index]--;
}
for (int i = 0; i < 26; i++) {
if (cache[i] != 0) { return false; }
}
return true;
}