Skip to content

Commit 4a1917e

Browse files
authored
Create 0409 Longest Palindrome.md
1 parent cc5ad2e commit 4a1917e

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

greedy/0409 Longest Palindrome.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# 409. Longest Palindrome
2+
https://leetcode-cn.com/problems/longest-palindrome/
3+
Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters.
4+
Letters are case sensitive, for example, "Aa" is not considered a palindrome here.
5+
6+
Example 1:
7+
Input: s = "abccccdd"
8+
Output: 7
9+
Explanation:
10+
One longest palindrome that can be built is "dccaccd", whose length is 7.
11+
12+
Example 2:
13+
Input: s = "a"
14+
Output: 1
15+
16+
Example 3:
17+
Input: s = "bb"
18+
Output: 2
19+
20+
Constraints:
21+
1 <= s.length <= 2000
22+
s consists of lowercase and/or uppercase English letters only.
23+
24+
``` python3
25+
class Solution:
26+
def longestPalindrome(self, s: str) -> int:
27+
d=collections.Counter(s)
28+
ans=0
29+
for k in d:
30+
ans+=d[k]//2*2
31+
if ans<len(s):
32+
ans+=1
33+
return ans
34+
```

0 commit comments

Comments
 (0)