916. Palindrome Permutation
class Solution {
public:
/**
* @param s: the given string
* @return: if a permutation of the string could form a palindrome
*/
bool canPermutePalindrome(string &s) {
// write your code here
const int size = s.size();
if(size == 0){
return true;
}
unordered_map<int, int> mp;
for(char c : s){
mp[c]++;
}
int evenCnt = 0, oddCnt = 0;
for(auto i : mp){
if(i.second % 2){
oddCnt++;
}
else{
evenCnt++;
}
}
if(oddCnt > 1){
return false;
}
else if(oddCnt == 1){
return size % 2;
}
else{
return true;
}
}
};
Comments
Post a Comment