-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathLeetcode_242_8.java
More file actions
33 lines (33 loc) · 1.01 KB
/
Leetcode_242_8.java
File metadata and controls
33 lines (33 loc) · 1.01 KB
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
class Solution {
public boolean isAnagram(String s, String t) {
// 优化,直接返回false
if (s.length() != t.length()) {
return false;
}
HashMap<Character, Integer> map = new HashMap<>();
// 构建hashmap
for (int i = 0; i<s.length(); i++) {
if (map.containsKey(s.charAt(i))) {
map.put(s.charAt(i), map.get(s.charAt(i))+1);
} else {
map.put(s.charAt(i), 1);
}
}
// 判断t
for (int i = 0; i<t.length(); i++) {
if (!map.containsKey(t.charAt(i))) {
return false;
} else {
map.put(t.charAt(i), map.get(t.charAt(i)) - 1);
}
}
System.out.println(map.get('a'));
for (int i = 0; i<s.length(); i++) {
if (map.get(s.charAt(i)) != 0){
System.out.println(s.charAt(i)+"不是0");
return false;
}
}
return true;
}
}