Java solution with int[52], neat, clean, beat 87%


  • 0
    W
    public int longestPalindrome(String s) {
        int[] count = new int[52];
        for(char ch : s.toCharArray()) {
            help(count, ch);
        }
        int ret = 0;
        int odd = 0;
        for(int n : count) {
            if(n%2==0) ret+=n;
            else {
                n -= 1;
                ret += n;
                odd++;
            }
        }
        
        if(odd > 0) ret+=1;
        return ret;
    }
    // A--Z a--z
    private void help(int[] num, char ch) {
        if(ch >= 'A' && ch <= 'Z') {
            num[ch-'A']++;
        } else {
            num[ch-'a'+26]++;
        }
    }

Log in to reply
 

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