Given a string, determine if a permutation of the string could form a palindrome.
Example 1:
Input: "code" Output: false
Example 2:
Input: "aab" Output: true
Example 3:
Input: "carerac" Output: true
class Solution {
public:
bool canPermutePalindrome(string s) {
map<char, int> hmap;
for(int i = 0; i < s.length(); i++)
{
char ch = s.at(i);
if(hmap.find(ch) != hmap.end())
{
hmap.erase(ch);
}
else
{
hmap[ch] = 1;
}
}
return hmap.size() <= 1;
}
};
'Computer General > Algorithm and Data Structure' 카테고리의 다른 글
Moving Average from Data Stream (0) | 2020.03.21 |
---|---|
Intersection of Three Sorted Arrays (0) | 2020.03.21 |
Nested List Weight Sum (0) | 2020.03.21 |