# LeetCode 409. Longest Palindrome

## Description

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.

## 描述

"abccccdd"

7

### 思路

• 统计所有单词出现的次数。
• 出现了偶数次的单词都以用来构成回文字符串，构成奇数次的单词减一次构成回文字符串。可以把奇数次的字符方中间，不需要和其它字符对应。因此奇数次的字符可以加一。

# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-10-03 14:41:25
from collections import Counter
class Solution:
def longestPalindrome(self, s: str) -> int:
cnt_dict = Counter(s)
a = sum(cnt_dict.values())
b = sum(1 for x in cnt_dict.values() if x & 1)
return a if b == 0 else a - b + 1

|
10月前
Leetcode 516. Longest Palindromic Subsequence

38 0
|
10月前
|
Java
Leetcode 3. Longest Substring Without Repeating Characters

41 3
LeetCode 424. Longest Repeating Character Replacem

98 0
LeetCode 395. Longest Substring with At Least K

79 0
|
Windows
LeetCode 388. Longest Absolute File Path

71 0
|

LeetCode 336. Palindrome Pairs

123 0
|

LeetCode 329. Longest Increasing Path in a Matrix

60 0
|

LeetCode 300. Longest Increasing Subsequence

43 0
|

LeetCode 214. Shortest Palindrome

77 0
|

LeetCode 128. Longest Consecutive Sequence

81 0