very simple java 9ms


  • 0
    public int longestPalindrome(String s) {
        int[] counts = new int[52];
        for(char c: s.toCharArray()){
            if(c-'a'>=0) {
                counts[c-'a']++;
            } else {
                counts[c-'A'+26]++;
            }
        }
        int sum = 0;
        for(int c: counts){
            if(c%2==0) {
                sum+=c;
            } else{
                sum+=(c-1);
            }
        }
        return sum+ (s.length()==sum?0:1);
    }

Log in to reply
 

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