Easy Java Solution !!


  • 0
    
    public class Solution {
        public int longestPalindrome(String s) {
            
            int[] dp = new int[52];
            int length = 0;
            
            for(char c: s.toCharArray()) {
                if(c >= 'a' && c <= 'z') dp[c - 'a']++;
                else dp[c - 'A' + 26]++;
            }
            
            int mid = 0, res = 0; 
            for( int i = 0; i < 52; i++ ) {
                //System.out.println( dp[i] );
                if( (dp[i] % 2 ) == 1 ) {
                    mid = 1;
                    dp[i]--;
                    i--;
                }
                else length += dp[i];
            }
            
            return length + mid ;
            
        }
    }

Log in to reply
 

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