O(n) c++ solution


  • 0
    M
    class Solution {
    public:
        bool canPermutePalindrome(string s) {
            vector<int> result(256,0);
            if(s.size()%2==0){
                for(int i=0;i<s.size();i++){
                    result[s[i]]++;
                }
                for(int i=0;i<256;i++){
                    if(result[i]%2 != 0)
                        return false;
                }
                return true;
            }
            else{
                int count = 0;
                for(int i=0;i<s.size();i++){
                    result[s[i]]++;
                }
                for(int i=0;i<256;i++){
                    if(result[i]%2 != 0)
                        count++;
                    if(count>1)
                        return false;
                }
                return true;
            }
        }
    };

Log in to reply
 

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