-
Notifications
You must be signed in to change notification settings - Fork 0
/
longestPalindrome.py
50 lines (41 loc) · 1.27 KB
/
longestPalindrome.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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.
# My Solution
class Solution:
def longestPalindrome(self, s: str) -> int:
counts = {}
for char in s:
if char not in counts:
counts[char] = 1
else:
counts[char] += 1
result = 0
foundExtra = False
for count in counts.values():
if count % 2 == 0:
result += count
else:
if not foundExtra:
result+=1
foundExtra = True
result += count - 1
return result
from collections import Counter
class BetterSolution:
def longestPalindrome(self, s):
ans = 0
for v in Counter(s).itervalues():
ans += v / 2 * 2
if ans % 2 == 0 and v % 2 == 1:
ans += 1
return ans