409. 最长回文串leetcode

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

在这里插入图片描述

发布了314 篇原创文章 · 获赞 23 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_39289876/article/details/104961045