C using an array


  • 0
    L

    This can be optimized using a bitset.

    bool canPermutePalindrome(char* s) {
        if (!s)
            return false;
        
        int* alph = (int *) calloc (256, sizeof (int));
        for (int c = 0; s[c] != '\0'; c++)
            alph[(int) s[c]]++;
        
        bool seen_middle = false;
        for (int c = 0; c < 256; c++) {
            if (alph[c] % 2 != 0) {
                if (!seen_middle)
                    seen_middle = true;
                else
                    return false;
            }
        }
        
        return true;
    }
    

Log in to reply
 

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