思路:最长回文串,就是统计能构成偶数对的能有几个字符,统计完后,如果有剩余字符,可以插在回文字符的中间。
利用长度为256的数组去统计总共每个字符有多少个
class Solution {
public int longestPalindrome(String s) {
int []cnts = new int[256] ;
for (char c : s.toCharArray())
cnts[c] ++;
int ans = 0;
for (int cnt : cnts)
ans += (cnt / 2) * 2;
if(ans < s.length()) ans ++;
return ans;
}
}