https://leetcode.com/problems/longest-palindrome
409. Longest Palindrome
给定一字串s 回传用s的字母能建立的最长回文字串的长度
Example 1:
Input: s = "abccccdd"
Output: 7
Explanation: One longest palindrome that can be built is "dccaccd", whose
length is 7.
Example 2:
Input: s = "a"
Output: 1
Explanation: The longest palindrome that can be built is "a", whose length is
1.
Constraints:
1 <= s.length <= 2000
s consists of lowercase and/or uppercase English letters only.
思路:
哈希表计数
Python Code:
class Solution:
    def longestPalindrome(self, s: str) -> int:
        record = defaultdict(int)
        for n in s:
            record[n] += 1
        count = 0
        one = 1
        for v in record.values():
            if v % 2 == 1:
                if one > 0:
                    one -= 1
                else:
                    count += 1
        return len(s) - count
又是ez的一天 舒服