6-lines C++ Solution using map, 0ms


  • 0
    F

    Even presence of a character is cancelled out, while odd presence of a character remains. If the number of characters with odd presence in the string is more than 1, then Palindrome is not possible. Otherwise, Palindrome is possible.

    bool canPermutePalindrome(string s) {
            map<char, int> result;
            for(char& c:s)
                if(result.count(c)==0)
                    result.insert(pair<char,int>(c,1));
                else result.erase(c);   
            return result.size()<=1;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.