Leetcode # 409. Longest Palindrome
- 2022.12.02
- LeetCode
https://leetcode.com/problems/longest-palindrome/
Solution
Palindrome: 回文
Time Complexity: O(len(s))
Space Complexity: O(1)
(The input and output generally do not count towards the space complexity.)
class Solution:
def longestPalindrome(self, s: str) -> int:
letter_count = collections.Counter()
for c in s:
letter_count[c] += 1
length = 0
has_odd = False
for count in letter_count.values():
length += count
if count % 2 == 1:
length -= 1
has_odd = True
return length + (1 if has_odd else 0)
Last Updated on 2023/08/16 by A1go