0 ms C solution


  • 1
    #define MAXSIZE 125 // the ascii code for lowercase z is 122
    
    int longestPalindrome(char* s) {
        int table[MAXSIZE] = {0}; // use a hash table to store the number of times a letter appears
        int len = strlen(s);
        int i = 0, ct = 0;
        while(i < len) {
            ++table[s[i]];
            if(table[s[i++]] % 2 == 0) // every time the number of times a letter appears reaches an even number, add 2
                ct += 2;
        }
        if(ct == len) // if all letters are used, return ct, also can return len
            return ct;
        return ct + 1; // or, return ct+1, because we can add one more letter to it.
    }
    

Log in to reply
 

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