[LeetCode]409. Longest Palindrome

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.

This is case sensitive, for example "Aa" is not considered a palindrome here.

Note:
Assume the length of given string will not exceed 1,010.

Example:

Input:
"abccccdd"

Output:
7

Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.


python3:
1 class Solution:
2     def longestPalindrome(self, s: str) -> int:
3         odd = 0
4         for i in set(s):
5             if s.count(i) % 2 != 0:
6                 odd += 1
7         return min(len(s), len(s) - odd + 1)

 

posted @ 2020-03-19 15:16  界757  阅读(112)  评论(0编辑  收藏  举报