Leetcode 409. Longest Palindrome

2021-07-23 16:20:47 浏览数 (1)

1. Description

2. Solution

**解析:**Version 1,统计字符个数,偶数的直接相加,奇数的减1相加,存在奇数则最终结果加1,即位于正中间。

  • Version 1
代码语言:javascript复制
class Solution:
    def longestPalindrome(self, s: str) -> int:
        stat = {}
        for ch in s:
            stat[ch] = stat.get(ch, 0)   1
        count = 0
        flag = False
        for v in stat.values():
            if v % 2 == 1:
                flag = True
                count  = v -1
            else:
                count  = v
        if flag:
            count  = 1
        return count

Reference

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

0 人点赞