AlgoMaster Logo

Longest Palindrome

easyFrequency6 min readUpdated June 23, 2026

Understanding the Problem

We're not asked to find a palindrome within the string. We're asked to build the longest possible palindrome using the characters available in the string. We can rearrange characters however we want, and we don't have to use all of them.

What makes a string a palindrome? It reads the same forwards and backwards. That means every character must appear in pairs, one for each side. The only exception is the middle character: if the palindrome has an odd length, exactly one character can appear an odd number of times (it sits in the center).

So the real question is how many characters we can use. Every character that appears an even number of times can be fully used. For characters that appear an odd number of times, we can use all but one of them (the even portion), and then place one leftover character in the middle. The length follows directly from the character counts.

Key Constraints:

  • 1 <= s.length <= 2000 → A single pass over the string is enough; the answer depends only on character counts, so an O(n) solution is the natural target.
  • s consists of lowercase and/or uppercase English letters only → At most 52 distinct characters (26 lowercase + 26 uppercase). The frequency map has a fixed, small size, which makes the per-character bookkeeping constant space.

Approach 1: Greedy with Hash Map

Intuition

We don't need to build the palindrome, only count how many characters can go into one.

A palindrome has a mirror structure: every character in the left half has a matching character in the right half. A character that appears an even number of times contributes all its copies. A character that appears an odd number of times contributes count - 1 copies (the largest even number below count), leaving one copy unused.

There is exactly one center position, so at most one unused copy can be placed in the middle. We sum the even contributions across all characters, then add 1 if any character had an odd count.

Algorithm

  1. Count the frequency of each character in the string using a hash map.
  2. Initialize length = 0 and hasOdd = false.
  3. For each character frequency:
    • Add freq / 2 * 2 to length (the largest even number <= freq). This is the even portion.
    • If freq is odd, set hasOdd = true.
  4. If hasOdd is true, add 1 to length (for the center character).
  5. Return length.

Example Walkthrough

s
1Initial string: count frequency of each character
0
a
1
b
2
c
3
c
4
c
5
c
6
d
7
d
freq
1Frequency map starts empty
1/7

Code

This approach counts every character first, then processes the counts in a second pass over the frequency array. The next approach computes the same answer in a single pass over the string, tracking parity directly instead of storing full counts.

Approach 2: Optimal (Single Pass with Set)

Intuition

Instead of counting frequencies and then analyzing them, we can use a set to track characters with odd counts as we go. When we see a character, if it's already in the set (meaning we've seen it an odd number of times), we remove it and add 2 to the length (a pair is complete). If it's not in the set, we add it, marking that we've seen it an odd number of times so far.

At the end, if the set is non-empty, there's at least one character with an odd count, so we can add 1 for the center.

Algorithm

  1. Initialize an empty set and length = 0.
  2. Iterate through each character in the string:
    • If the character is already in the set, remove it and add 2 to length (a pair is formed).
    • Otherwise, add the character to the set.
  3. If the set is non-empty after the loop, add 1 to length (center character).
  4. Return length.

Example Walkthrough

s
1Initialize: length=0, oddChars={}, scan left to right
0
a
i
1
b
2
c
3
c
4
c
5
c
6
d
7
d
oddChars
1Set empty, length=0
1/7

Code