面试题 01.04. 回文排列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public boolean canPermutePalindrome(String s) {
Map<Character, Integer> charToCountMap = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
charToCountMap.put(c, charToCountMap.getOrDefault(c, 0) + 1);
}

int oddCount = 0;
for (int value : charToCountMap.values()) {
if ((value & 1) == 1) {
if (oddCount++ >= 1) {
return false;
}
}
}

return true;
}
}

References

面试题 01.04. 回文排列