> For the complete documentation index, see [llms.txt](https://joshualbarb.gitbook.io/leetcode-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://joshualbarb.gitbook.io/leetcode-problems/strings/string-simulation/longest-palindrome.md).

# 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.

![](/files/-MR131eXMLWNffQ2-gyj)

### Thought Process

![](/files/-MR13MPszLEtAMxSCnzq)

## 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
