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 maxLength = 0; Set<Character> set = new HashSet<>(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (set.contains(c)) { set.remove(c); maxLength += 2; } else { set.add(c); } }
return set.isEmpty() ? maxLength : maxLength + 1; } }
|