-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode242.java
More file actions
28 lines (25 loc) · 925 Bytes
/
leetcode242.java
File metadata and controls
28 lines (25 loc) · 925 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
import java.util.HashMap;
public class leetcode242 {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) return false;
HashMap<Character, Integer>sCount = new HashMap<>();
HashMap<Character, Integer>tCount = new HashMap<>();
for(int i = 0; i < s.length(); i++){
sCount.put(s.charAt(i),sCount.getOrDefault(s.charAt(i), 0) + 1);
tCount.put(t.charAt(i), tCount.getOrDefault(t.charAt(i), 0)+1);
}
for(int i = 0; i < s.length(); i++){
char key = s.charAt(i);
if(!sCount.get(key).equals(tCount.get(key))){
return false;
}
}
return true;
}
public static void main(String[] args) {
String s = "anagram";
String t = "nagaram";
leetcode242 Solution = new leetcode242();
System.out.println(Solution.isAnagram(s, t));
}
}