看清题目,没让你构造输出具体的回文串字符,只是让统计长度。
import collections
class Solution:
def longestPalindrome(self, s: str) -> int:
dic = collections.Counter(s)
ans = 0
flag = 1
for num in dic.values():
ans += num // 2 * 2
if num % 2 == 1 and flag:
ans += 1
flag = 0
return ans