1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Solution { public int longestPalindrome(String s) { int[] result = new int[58]; for (int i = 0; i < s.length(); i++) { result[s.charAt(i) - 'A'] += 1; } int maxLen = 0; for (int i = 0; i < 58; i++) { if (result[i] % 2 == 0) { maxLen += result[i]; } else { maxLen += (result[i] - 1); } } return maxLen < s.length() ? maxLen + 1 : maxLen; } }
|