LeetCode 409 Longest Palindrome (Python)

Posted by 小明MaxMing on August 14, 2020

题目

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.

解题思路

如果一个字母出现偶数次,则都可以用,奇数次,则可以用次数-1,然后最中间放一个出现奇数次的字母

代码

class Solution:
    def longestPalindrome(self, s: str) -> int:
        ct = Counter(s)
        res = 0
        f = 0
        for v in ct.values():
            if v % 2 == 0:
                res += v
            else:
                res += v - 1
                f = 1
        return res + f

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅