题目:
给定一个包含大写字母和小写字母的字符串 s ,返回 通过这些字母构造成的 最长的回文串 。
在构造过程中,请注意 区分大小写 。比如 "Aa" 不能当做一个回文字符串。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/longest-palindrome
示例 1:
输入:s = "abccccdd" 输出:7 解释: 我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。
class Solution:
def longestPalindrome(self, s):
if len(set(s))==1:
return len(s)
a=0
b=[]
c=[]
for i in s:
if s.count(i)%2==0:
if i not in b:
a+=s.count(i)
b.append(i)
elif s.count(i)%2==1 and s.count(i)!=1:
if i not in b:
a+=(s.count(i)-1)
b.append(i)
count=0
for i in s:
c.append(i)
for i in s:
if s.count(i)%2==0:
count+=1
if count==len(c):
return a
else:
return a+1