forked from kdn251/interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidAnagram.java
More file actions
30 lines (27 loc) · 744 Bytes
/
ValidAnagram.java
File metadata and controls
30 lines (27 loc) · 744 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
class ValidAnagram {
public boolean isAnagram(String s, String t) {
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for(char c: s.toCharArray()) {
if(map.containsKey(c)) {
map.put(c, map.get(c) + 1);
}
else {
map.put(c, 1);
}
}
for(char c: t.toCharArray()) {
if(map.containsKey(c)) {
map.put(c, map.get(c) - 1);
}
else {
return false;
}
}
for(char c: map.keySet()) {
if(map.get(c) != 0) {
return false;
}
}
return true;
}
}