Skip to content

Commit 4f0eea2

Browse files
committed
Longest Palindrome
1 parent 57f97de commit 4f0eea2

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Longest_Palindrome.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Given a string which consists of lowercase or uppercase letters, find the
2+
# length of the longest palindromes that can be built with those letters.
3+
#
4+
# This is case sensitive, for example "Aa" is not considered a palindrome here.
5+
#
6+
# Note:
7+
# Assume the length of given string will not exceed 1,010.
8+
#
9+
# Example:
10+
#
11+
# Input:
12+
# "abccccdd"
13+
#
14+
# Output:
15+
# 7
16+
#
17+
# Explanation:
18+
# One longest palindrome that can be built is "dccaccd", whose length is 7.
19+
20+
import collections
21+
22+
23+
class Solution:
24+
def longestPalindrome(self, s):
25+
dict = collections.Counter(s)
26+
ans = 0
27+
for value in dict.values():
28+
ans += value // 2 * 2
29+
if ans % 2 == 0 and value % 2 == 1:
30+
ans += 1
31+
return ans

0 commit comments

Comments
 (0)