3 ms C++ histogram based solution, O(1) space O(n) time


  • 0
    V

    The idea here is that all the even occurrence if any given char cancel each other and we can have at most one char occurring odd number of times in palindrome.

    bool canPermutePalindrome(string s) {
            vector<bool> h(256, false);
            for (auto x : s) 
                h[x] = !h[x];
            int cnt=0;
            for (auto x : h)
                if (x) cnt++;
            return cnt < 2;
        }
    

Log in to reply
 

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