class Solution {
public int longestPalindrome(String s) {
if (s == null || "".equals(s)) {
return 0;
}
int[] counts = new int[75];
for (char c : s.toCharArray()) {
counts[c - '0']++;
}
int length = 0;
boolean hasOdd = false;
for (int count : counts) {
if (count == 0) {
continue;
}
length += count / 2 * 2;
if (!hasOdd && count % 2 == 1) {
hasOdd = true;
}
}
if (hasOdd) {
length += 1;
}
return length;
}
}
image.png
网友评论