-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsAnagram.java
More file actions
36 lines (31 loc) · 1001 Bytes
/
Copy pathIsAnagram.java
File metadata and controls
36 lines (31 loc) · 1001 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
28
29
30
31
32
33
34
35
36
package org.example;
import java.util.HashMap;
public class IsAnagram {
public boolean isAnagram(String s, String t) {
HashMap<Character, Integer> hashMap = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
if (hashMap.containsKey(s.charAt(i))) {
// TOOD 如何对value + 1
int count = hashMap.get(s.charAt(i));
count++;
hashMap.put(s.charAt(i), count);
} else {
hashMap.put(s.charAt(i), 1);
}
}
for (int i = 0; i < t.length(); i++) {
if (!hashMap.containsKey(t.charAt(i))) {
return false;
} else {
int count = hashMap.get(t.charAt(i));
if (count > 0) {
count--;
hashMap.put(t.charAt(i), count);
} else {
return false;
}
}
}
return true;
}
}