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
A:class Solution { public: bool canPermutePalindrome(string s) { unordered_map<char, int> M; for(auto c : s) M[c]++; int oddCount = 0; for(auto iter = M.begin(); iter!= M.end(); iter++){ if(iter->second %2) oddCount++; } return oddCount<=1; } };
Mistakes:
No comments:
Post a Comment