# 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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://joshualbarb.gitbook.io/leetcode-problems/strings/string-simulation/longest-palindrome.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
