-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path242.有效的字母异位词.java
39 lines (36 loc) · 1.15 KB
/
242.有效的字母异位词.java
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
34
35
36
37
38
/*
* @lc app=leetcode.cn id=242 lang=java
*
* [242] 有效的字母异位词
*/
// @lc code=start
class Solution {
public boolean isAnagram(String s, String t) {
// mine
// if (s.length() != t.length()) return false;
// Map<Character, Integer> counter = new HashMap<Character, Integer>();
// for (char c : s.toCharArray()) {
// int count = counter.getOrDefault(c, 0);
// counter.put(c, ++count);
// }
// for (char c : t.toCharArray()) {
// int count = counter.getOrDefault(c, 0);
// if (count == 0) return false;
// if (count == 1) counter.remove(c);
// else counter.put(c, --count);
// }
// return counter.isEmpty();
// Solution from Nick White
if (s.length() != t.length()) return false;
int[] charCounts = new int[26];
for (int i = 0 ; i < s.length() ; i++) {
charCounts[s.charAt(i) - 'a']++;
charCounts[t.charAt(i) - 'a']--;
}
for (int count : charCounts) {
if (count != 0) return false;
}
return true;
}
}
// @lc code=end