本题题目要求如下:
Given a string, determine if a permutation of the string could form a palindrome.
For example,
"code" -> False, "aab" ->
True, "carerac" -> True.
class Solution {
public:
bool canPermutePalindrome(string s) {
vector<int> hashmap(256, 0);
for (int i = 0; i < s.length(); ++i) {
hashmap[s[i]]++;
}
int cnt = 0;
for (int i = 0; i < 256; ++i) {
if (hashmap[i] % 2 == 1) {
++cnt;
}
}
return !(cnt > 1);
}
};
本文介绍了一种使用哈希映射的方法来判断给定字符串的任意排列是否能构成回文串。通过统计每个字符出现的次数,确保最多只有一个字符出现奇数次,即可快速得出结论。
184

被折叠的 条评论
为什么被折叠?



