-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
33 lines (23 loc) · 753 Bytes
/
Solution.java
File metadata and controls
33 lines (23 loc) · 753 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
package leetcode.ValidAnagram;
import java.util.HashMap;
import java.util.Map;
class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) return false;
Map<Character, Integer> freq = new HashMap<>();
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
int frequenceChar = freq.getOrDefault(c, 0) + 1;
freq.put(c, frequenceChar);
}
for(int i = 0; i < t.length(); i++){
char c = t.charAt(i);
int frequenceChar = freq.getOrDefault(c, 0);
if(frequenceChar <= 0){
return false;
}
freq.put(c, frequenceChar - 1);
}
return true;
}
}