409. Longest Palindrome
Input:
"abccccdd"
Output:
7
Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.解题要点:
class Solution {
public int longestPalindrome(String s) {
int[] count = new int[128];
for(char c : s.toCharArray()){
count[c]++;
}
int res = 0, odd = 0;
for(int cc : count){
res += cc;
if(cc % 2 == 1){
res -= 1;
odd += 1;
}
}
int add = 0;
if(odd > 0) add = 1;
return res + add;
}
}Last updated