https://leetcode.com/problems/longest-palindrome
409. Longest Palindrome
給定一字串s 回傳用s的字母能建立的最長回文字串的長度
Example 1:
Input: s = "abccccdd"
Output: 7
Explanation: One longest palindrome that can be built is "dccaccd", whose
length is 7.
Example 2:
Input: s = "a"
Output: 1
Explanation: The longest palindrome that can be built is "a", whose length is
1.
Constraints:
1 <= s.length <= 2000
s consists of lowercase and/or uppercase English letters only.
思路:
哈希表計數
Python Code:
class Solution:
def longestPalindrome(self, s: str) -> int:
record = defaultdict(int)
for n in s:
record[n] += 1
count = 0
one = 1
for v in record.values():
if v % 2 == 1:
if one > 0:
one -= 1
else:
count += 1
return len(s) - count
又是ez的一天 舒服