问题描述

思路解析
- 因为同时包含大小写字母,直接创建个ASCII表大小的桶来标记
- 又因为是要回文子串,所以偶数个数的一定可以
- 那么同时,对于出现奇数次数的,我没需要他们的次数-1,变为偶数,并且可以标记出现过奇数的数字,这样可以放到中间,结果+1即可
代码
class Solution {
    public int longestPalindrome(String s) {
        int[] asc = new int[128];
        for (char ch : s.toCharArray()) {
            asc[ch]++;
        }
        int res = 0;
        boolean flag = false;
        for (int i = 0; i < 128; i++) {
            if (asc[i] != 0 && asc[i] % 2 == 0) {
                res += asc[i];
            } else if (asc[i] % 2 == 1) {
                flag=true;
                res += asc[i] - 1;
            }
        }
        return flag==true?res+1:res;
    }
}


















