Longest Palindrome
Problem
Given a string s
which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters.
Letters are case sensitive, for example, "Aa"
is not considered a palindrome here.

Thought Process

Solution
class Solution:
def longestPalindrome(self, s: str) -> int:
ss = set()
count = 0
for letter in s:
if letter not in ss:
ss.add(letter)
else:
ss.remove(letter)
count+=1
if len(ss) != 0:
return count*2+1
return count*2
Time Complexity
Time: O(n)
Space: O(1) because the hash set is bounded by 26 characters
Last updated
Was this helpful?