Longest Palindrome - Problem

Imagine you have a collection of letter tiles, each with either a lowercase or uppercase letter. Your task is to build the longest possible palindrome using these tiles!

A palindrome reads the same forwards and backwards, like "racecar" or "A man a plan a canal Panama" (ignoring spaces). However, in this problem, case matters - so "Aa" is not a palindrome because uppercase 'A' and lowercase 'a' are different characters.

Goal: Given a string s containing letters, return the length of the longest palindrome you can construct using those letters.

Example: With letters "abccccdd", you can build "dccaccd" (length 7) by using pairs of letters symmetrically and putting one odd-count letter in the middle.

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "abccccdd"
โ€บ Output: 7
๐Ÿ’ก Note: We can construct palindrome "dccaccd" using: 2 pairs of 'c', 1 pair of 'd', and 1 'a' in the center. Total length = 4 + 2 + 1 = 7.
example_2.py โ€” Single Character
$ Input: s = "a"
โ€บ Output: 1
๐Ÿ’ก Note: The palindrome is just "a" itself, so the length is 1.
example_3.py โ€” All Same Characters
$ Input: s = "aaaa"
โ€บ Output: 4
๐Ÿ’ก Note: We can use all 4 'a's to construct palindrome "aaaa". Since count is even, no center character needed.

Visualization

Tap to expand
Building a Palindrome: The Mirror House ApproachInput: "abccccdd" โ†’ Build longest palindromePalindrome ConstructionLeft Side (Pairs)dccCenteraRight Side (Mirror)ccdStep-by-step Process:1. Count frequencies: a(1), b(1), c(4), d(2)2. Calculate pairs: a(0), b(0), c(2), d(1) = total 3 pairs3. Pairs contribute: 3 ร— 2 = 6 characters4. Odd counts available: a(1), b(1) โ†’ can use 1 for center5. Final result: 6 + 1 = 7 charactersโœ“ Palindrome "dccaccd" has length 7
Understanding the Visualization
1
Count Building Materials
Survey all available letters and count how many of each type you have
2
Form Symmetric Pairs
For each letter type, determine how many symmetric pairs you can create
3
Choose Center Piece
If any letter has leftover count, pick one as the unique center element
4
Calculate Total Length
Sum all pairs (ร—2 for symmetry) plus 1 if using a center piece
Key Takeaway
๐ŸŽฏ Key Insight: A palindrome uses character pairs symmetrically, with at most one odd-frequency character in the center. Count frequencies once, then sum pairs and add 1 if any odd count exists.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the string to count frequencies, then O(k) to process counts where k โ‰ค min(n, 128) for ASCII

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Hash table stores at most k unique characters where k โ‰ค min(n, 128) for ASCII characters

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค s.length โ‰ค 2000
  • s consists of lowercase and/or uppercase English letters only
  • Case sensitive: 'A' and 'a' are considered different characters
Asked in
Google 23 Amazon 18 Meta 15 Apple 12 Microsoft 10
127.9K Views
High Frequency
~8 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen