Beats 94% submissions 9 ms


  • 0
    P
    public class Solution {
        public int longestPalindrome(String s) {
            char[] sc = s.toCharArray();
            int [] alphabets = new int[58]; int count =0;
            for(char c: sc) alphabets[(int)c-(int)'A']++;
            for(int i:alphabets) { if(i==s.length()) return i;
            if(i%2 ==0) count = count+i;
            else if(i>2) count = count+i-1;    
            }
            if(count == s.length()) return count;
            else
            return count+1;
        }
    }

Log in to reply
 

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