409. Longest Palindrome

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.

This is case sensitive, for example "Aa" is not considered a palindrome here.

Note:
Assume the length of given string will not exceed 1,010.

Example:

Input:
"abccccdd"

Output:
7

Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.
class Solution {
    public int longestPalindrome(String s) {
        if(s == null || s.length() == 0) return 0;
        Set<Character> set = new HashSet();
        int res = 0;
        
        for(char c: s.toCharArray()){
            if(set.contains(c)){
                res++;
                set.remove(c);
            }
            else set.add(c);
        }
        return set.size() > 0 ? 2*res+1 : 2*res;
    }
}

草,问的原来是能用里面的letters组成的最长palindrome。直抒胸臆

原文地址:https://www.cnblogs.com/wentiliangkaihua/p/12928164.html