LeetCode—Python—409. 最长回文串

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/IOT_victor/article/details/88763281

1、题目描述

字符串(409):

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

给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。

在构造过程中,请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。

注意:
假设字符串的长度不会超过 1010。

示例 1:

输入:
"abccccdd"

输出:
7

解释:
我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。

2、代码详解

class Solution(object):
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: int
        """
        """
        :type s: str
        :rtype: int
        """
        hash = set()
        for c in s:
            if c not in hash:
                hash.add(c)
            else:
                hash.remove(c)
        # len(hash):奇数出现的个数
        return len(s) - len(hash) + 1 if len(hash) > 0 else len(s)

猜你喜欢

转载自blog.csdn.net/IOT_victor/article/details/88763281