-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ8.java
More file actions
32 lines (25 loc) · 671 Bytes
/
Q8.java
File metadata and controls
32 lines (25 loc) · 671 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
//using hashmap as a hint
class Solution {
public boolean isAnagram(String s, String t) {
//obvious length check
if (s.length() != t.length()){
return false;
}
HashMap<Character,Integer> map = new HashMap<>();
//build using s
for (char ch: s.toCharArray()){
map.put(ch,map.getOrDefault(ch,0)+1);
}
//using t
for (char ch : t.toCharArray()){
if (!map.containsKey(ch)){
return false;
}
map.put(ch, map.get(ch)-1);
if(map.get(ch)<0){
return false;
}
}
return true;
}
}