classSolution{publicintlongestPalindrome(String s){
HashMap<String,Integer> map =newHashMap<>();int len = s.length();if(len==0)return0;if(len==1)return1;for(int i=0;i<len;i++){
String ss = s.charAt(i)+"";if(map.containsKey(ss)){
map.put(ss,map.get(ss)+1);}else{
map.put(ss,1);}}int max =0;for(Map.Entry<String,Integer> entry:map.entrySet()){if(entry.getValue()%2!=0){
len--;
max =1;}}return max==1?len+1:len;}}
题解做法:用数组存储字母出现次数。
classSolution{publicintlongestPalindrome(String s){int[] count =newint[128];for(char c: s.toCharArray())
count[c]++;int ans =0;for(int v: count){
ans += v /2*2;if(v %2==1&& ans %2==0)
ans++;}return ans;}}