lintcode:最长回文串

xiaoxiao2021-02-28  86

class Solution: # @param {string} s a string which consists of lowercase or uppercase letters # @return {int} the length of the longest palindromes that can be built def longestPalindrome(self, s): # Write your code here if not s: return 0 a = set(s) dict_str = {} for i in a: # 创建字典 dict_str[i] = s.count(i) # print(dict_str) re_1, re_2, re_3 = 0, 0, 0 for key in dict_str: if dict_str[key] % 2 == 0: # 双数的key re_2 += dict_str[key] else: re_1 = re_1 + dict_str[key] - 1 # 单数的key re_3 = 1 # 有单数时回文的中间数 return re_1 + re_2 + re_3
转载请注明原文地址: https://www.6miu.com/read-25179.html

最新回复(0)