Java solution with int[58]


  • 0
    C
     public int longestPalindrome(String s) {
            int[] charMap = new int[58];
            int length = 0;
            boolean odd = false;
            for(int i = 0; i < s.length(); i++){
                int ch = s.charAt(i) - 'A';
                charMap[ch] += 1;
            }
            for(int c : charMap) {
                if(c%2 == 1){
                    odd = true;
                    length += c - 1;
                }else length += c;
            }
            return odd? length + 1 : length;
        }
    

Log in to reply
 

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