Problem
Given a string, write a function to check if it is a permutation of a palin drome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words.
Example
Input: “tactcoa”
Output: true(permutations: “tacocat”、“atcocta”, etc.)
Solution
回文中,最多只有一个字符的个数是奇数。
class Solution {
public:
bool canPermutePalindrome(string s) {
unordered_map<char,int> hash_table;
for(auto c:s)
{
hash_table[c]++;
}
int odd_cnt = 0;
for(unordered_map<char,int>::const_iterator it = hash_table.begin();
it != hash_table.end();++it)
{
if(it->second % 2 == 1)
{
++odd_cnt;
if(odd_cnt > 1)
{
return false;
}
}
}
return true;
}
};