public class Solution {
public boolean canPermutePalindrome(String s) {
if (s == null || s.length() < 2) {
return true;
}
Map<Character, Boolean> map = new HashMap<>();
int oddCount = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!map.containsKey(c)) {
map.put(c, false);
oddCount++;
} else {
if (map.get(c)) {
map.put(c, false);
oddCount++;
} else {
map.put(c, true);
oddCount--;
}
}
}
boolean evenStr = s.length() % 2 == 0;
if (evenStr) {
if (oddCount == 0) {
return true;
} else {
return false;
}
} else {
if (oddCount == 1) {
return true;
} else {
return false;
}
}
}
}
Palindrome Permutation
最新推荐文章于 2022-10-31 14:52:19 发布