Faaez Razeen

Longest Palindrome

  • 1 min read
  • String
  • LC-Easy

3 years ago

Solution

TimeSpaceExplanation
O(n)O(1)Space is constant because of limited character set
def longestPalindrome(self, s: str) -> int: counts = Counter(s) ans = 0 odd = False for ch in counts: count = counts[ch] if count % 2 == 0: ans += count else: odd = True ans += count - 1 if odd: ans += 1 return ans