1问题描述
2题解-
时间复杂度O(n)遍历了一遍,空间复杂度O(S),S是字符集的大小,这也不知道算啥方法,简单的逻辑解法·
class Solution:
def longestPalindrome(self, s: str) -> int:
s_counter=collections.Counter(s)
ans=0
odd_exit=0
for alpha in s_counter:
if not (s_counter[alpha]%2):
ans+=s_counter[alpha]
else:
odd_exit=1
if s_counter[alpha]>1:
ans+=s_counter[alpha]-1
if odd_exit==1:
return ans+1
else:
return ans