409. 最长回文串

2020-03-19  本文已影响0人  上杉丶零
class Solution {
    public int longestPalindrome(String s) {
        if (s == null || "".equals(s)) {
            return 0;
        }

        int[] counts = new int[75];

        for (char c : s.toCharArray()) {
            counts[c - '0']++;
        }

        int length = 0;
        boolean hasOdd = false;

        for (int count : counts) {
            if (count == 0) {
                continue;
            }

            length += count / 2 * 2;

            if (!hasOdd && count % 2 == 1) {
                hasOdd = true;
            }
        }

        if (hasOdd) {
            length += 1;
        }

        return length;
    }
}
image.png
上一篇下一篇

猜你喜欢

热点阅读