Longest Palindrome
Input:
"abccccdd"
Output:
7
Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.题目大意:
解题思路:
Last updated
Input:
"abccccdd"
Output:
7
Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.Last updated
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: int
"""
"""
hashmap = {}
for item in s:
if item in hashmap:
hashmap[item] += 1
else:
hashmap[item] = 1
"""
hashmap = collections.Counter(s)
result = 0
odd = 0
for item in hashmap:
if hashmap[item] % 2 == 0:
result += hashmap[item]
else:
result += hashmap[item] - 1
odd = 1
return result + oddclass Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: int
"""
hashmap = collections.Counter(s)
result = 0
odd = 0
for item in hashmap:
result += hashmap[item]
if hashmap[item] % 2 == 1:
result -= 1
odd = 1
return result + odd例如:>>> # Tally occurrences of words in a list
>>> cnt = Counter()
>>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
... cnt[word] += 1
>>> cnt
Counter({'blue': 3, 'red': 2, 'green': 1})