9ms Clean easy Java Solution using int[58]


  • 0
    J
    public int longestPalindrome(String s) {
            int[] freq = new int[58];
            for (char c : s.toCharArray()) {
                freq[c - 'A']++;
            }
            int maxLen = 0;
            for (int f : freq) {
                maxLen += (f / 2) * 2;
            }
            return maxLen == s.length() ? maxLen : maxLen + 1;
        }
    

    In ASCII, there are some characters between lowercase and uppercase letters. Thus using 58 length arr instead of 52 length.


Log in to reply
 

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