Longest Palindrome

https://leetcode.com/problems/longest-palindrome/

class Solution {
    public int longestPalindrome(String s) {
        int[] count = new int[256];
        for (char c : s.toCharArray()) {
            count[c]++;
        }
        int total = 0;
        int odd = 0;
        for (int c : count) {
            if (c % 2 == 0) {
                total += c;
            } else {
                total += c - 1;
                odd = 1;
            }
        }
        return total + odd;
    }
}

最后更新于

这有帮助吗?