clueless coding // TODO: be smarter

LeetCode Palindrome Permutation: CPP Solution



TL;DR Code Solution


bool canPermutePalindrome(string s) {
    unordered_map<char, int> map;

    for (int i = 0; i < s.length(); i++) {
        map[s[i]]++;
    }

    bool hasCenter = false;
    for (unordered_map<char,int>::iterator it = map.begin(); it != map.end(); it++) {
        if (it->second % 2 != 0) {
            if (!hasCenter) {
                hasCenter = true;
            }
            else {
                return false;
            }
        }
    }

    return true;
}