C++_Hash Table_0ms


  • 0
    class Solution {
    public:
    bool canPermutePalindrome(string s) {
        if(s.empty()) return true;
        unordered_map<char, int> count;
        for(int i = 0; i < s.size(); ++i){
            if(count.find(s[i]) == count.end()){
                count[s[i]] = 1;
            }else{
                ++count[s[i]];
            }
        }
        
        int odd = 0;
        for(int i = 0; i < s.size(); ++i){
             if(count[s[i]] % 2 != 0){++odd; ++count[s[i]];}// to avoid duplicate count for the odd number
        }
        
        return odd <= 1;
    }
    };

Log in to reply
 

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