266. Palindrome Permutation

Problem:

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

Solutions:

class Solution {
public:
    bool canPermutePalindrome(string s) {
        int charCount[256] = {0};
        for (auto c : s) {
            charCount[c] ^= 0x1;
        }
        int count = 0;
        for (int i = 0; i < 256; ++i) {
            count += charCount[i];
        }

        return s.length() % 2 == 0 ? count == 0 : count == 1;
    }
};

results matching ""

    No results matching ""