916. Palindrome Permutation

Code(Language:C++) (Judger:cloudjudge-cluster-9)
class Solution {
public:
    /**
     * @param s: the given string
     * @return: if a permutation of the string could form a palindrome
     */
    bool canPermutePalindrome(string &s) {
        // write your code here
        const int size = s.size();
        if(size == 0){
            return true; 
        }
        unordered_map<int, int> mp;
        for(char c : s){
            mp[c]++; 
        }
        int evenCnt = 0, oddCnt = 0; 
        for(auto i : mp){
            if(i.second % 2){
                oddCnt++; 
            }
            else{
                evenCnt++; 
            }
        }
        if(oddCnt > 1){
            return false; 
        }
        else if(oddCnt == 1){
            return size % 2; 
        }
        else{
            return true; 
        }
    }
};

Comments

Popular posts from this blog

1427. Split Array into Fibonacci Sequence

Amazon OA 763. Partition Labels

05/25 周一