Java Solution


  • 0
    Y
     public int longestPalindrome(String s) {
            
            int[] c = new int[52];
            int l=0;
            boolean one=false;
            for(int i=0; i<s.length(); i++)
            {
                if('z' >= s.charAt(i) && s.charAt(i) >= 'a')    
                    c[s.charAt(i)-'a']++;
                else if('Z' >= s.charAt(i) && s.charAt(i) >= 'A')    
                    c[s.charAt(i)-'A'+26]++;
            }
            for(int i=0; i<52; i++)
            {
                if(c[i] %2 == 0)
                    l+=c[i];
                else if(c[i] > 2)
                {
                    one=true;
                    l+=c[i]-1;
                } else if(c[i] > 0)
                    one=true;
                    
            }
            return one?l+1:l;
        }
    

Log in to reply
 

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