#409

Longest Palindrome

Easy Frequency count

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Using the letters of s (case-sensitive), return the length of the longest palindrome you can build.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: using these letters, how long a palindrome can you build?

Why this works: pairs go on both sides; one leftover odd letter can sit in the middle.

Example: "abccccdd"

c4 → 4, d2 → 2, a1 b1 → odd6 + 1 = 7 ("dccaccd")

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Adding every odd count fully — only one odd letter can be in the middle.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(charset).
Topic: Strings
🎬 Episodes 16–20: video coming soon