通过奇偶数来判断回文,很好。
public class Solution {
public boolean canPermutePalindrome(String s) {
if (s == null || s.length() < 2) {
return true;
}
int oddCount = 0;
Map<Character, Boolean> map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (map.containsKey(c)) {
if (map.get(c)) {
map.put(c, false);
oddCount++;
} else {
map.put(c, true);
oddCount--;
}
} else {
map.put(c, false);
oddCount++;
}
}
int mod = s.length() % 2;
if (mod == 0) {
if (oddCount == 0) {
return true;
} else {
return false;
}
} else {
if (oddCount == 1) {
return true;
} else {
return false;
}
}
}
}