Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
This is case sensitive, for example"Aa"
is not considered a palindrome here.
Note:
Assume the length of given string will not exceed 1,010.
记录字符出现的次数,偶数次的直接加入结果,奇数次的话加入出现次数减一,并把unique设为1
class Solution {
public int longestPalindrome(String s) {
if(s == null || s.length() == 0)
return 0;
int[] count = new int[128];
for(int i = 0; i < s.length(); i++)
count[s.charAt(i)]++;
int res = 0;
int unique = 0;
for(int i = 0; i < count.length; i++){
if(count[i] % 2 == 0) {
res += count[i];
} else {
res += count[i] - 1;
unique = 1;
}
}
return res + unique;
}
}