Simple Java Solution


  • 0
    public class Solution {
        public int longestPalindrome(String s) {
            int[] hash = new int[52];
            
            int len = s.length(),
                longest_length = 0,
                odd_count = 0;
            
            for(int i=0;i<len;++i)
            {
                if(s.charAt(i) >= 'A' && s.charAt(i) <= 'Z')
                {
                    hash[s.charAt(i)-'A']++;
                }
                else
                {
                    hash[s.charAt(i)-'a' + 26]++;
                }
            }
            
            
            for(int i=0;i<hash.length;++i)
            {
                longest_length += hash[i];
                
                if(hash[i] % 2 == 1)
                    ++odd_count;
            }
            
            return odd_count == 0 ? longest_length : longest_length - (odd_count - 1);
        }
    }
    

Log in to reply
 

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