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
lintcode:最长回文串
最新推荐文章于 2019-03-24 17:24:23 发布