一、题目描述
二、解题思路
就是用一个Hash
表存储字符串中出现过的字符,待字符串遍历完成后,再遍历一遍Hash
表,如果出现次数为奇数的字母超过1
个,就不可能重组为回文串,否则就可以重组为回文串。
三、解题代码
class Solution {
public:
bool canPermutePalindrome(string s) {
vector<int> Hash(128, 0);
unsigned int how_much_single = 0;
for(size_t i = 0; i < s.size(); i++) Hash[s[i]]++;
for(auto it: Hash)
if(it % 2) how_much_single++;
return how_much_single <= 1;
}
};