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.
Example:
Input: "abccccdd" Output: 7 Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
ASCII,小写字母比大写字母大32。直接略成58,中间多的6个照算(实际没用到)。
public class Solution {
public int longestPalindrome(String s) {
int[] num = new int[58];
for(int i=0;i<s.length();i++){
num[s.charAt(i)-'A']++;
}
int sum=0;
boolean hasSingle=false;
for(int i=0;i<58;i++){
if(num[i]%2==1){
sum+=num[i]-1;
hasSingle=true;
}else if(num[i]!=0){
sum+=num[i];
}
}
return hasSingle?sum+1:sum;
}
}