--- tags: leetcode --- # [266. Palindrome Permutation](https://leetcode.com/problems/palindrome-permutation/) --- # My Solution ## The Key Idea for Solving This Coding Question If a string `s` can become a palindrome, there is exactly one character with odd number of occurences in `s`. ## C++ Code ```cpp= class Solution { public: bool canPermutePalindrome(string s) { vector<int> charCnt(26, 0); for (auto &c : s) { ++charCnt[c - 'a']; } int oddCnt = 0; for (int i = 0; i < charCnt.size(); ++i) { if (charCnt[i] & 0x1) { ++oddCnt; if (oddCnt > 1) { return false; } } } return true; } }; ``` ## Time Complexity $O(n)$ $n$ is the length of `s`. ## Space Complexity $O(1)$ # Miscellaneous <!-- # Test Cases ``` "code" ``` ``` "aab" ``` ``` "carerac" ``` ``` "as" ``` -->