DSA Factory
Free Mixed sets lessonsMixed sets · Stage 0 · Set 1 · Step 2

Build a palindrome

The longest palindrome you can make from a pile of letters. About 10 minutes.

No label this time

From here the steps don't name the idea. Before you write any code, ask yourself: does the order of the characters matter here, or only what the string is made of? Is it about counts, two ends, a window, or reading pieces?

A good habit is to say in one sentence what the answer depends on. If that sentence doesn't mention positions, you probably don't need them.

Say it in one sentence: what does the answer depend on?
s = "abccccdd"
abcd
count
1
1
4
2
length
0

Order doesn't matter, so count: a 1, b 1, c 4, d 2.

Move 1 of 4
Quick check

You may rearrange the letters. What decides how long a palindrome you can build?

  1. AHow many of each letter you have
  2. BThe order the letters come in
  3. CThe first and last letters
Show the answer

How many of each letter you have. Order is yours to choose, so only the counts matter.

Your problem

Build a palindrome

You get a string of letters (capital and small letters are different). Using each character at most once, in any order, return the length of the longest palindrome you can build.

Example
s = "abccccdd" → 7

0 ≤ length ≤ 100,000 · letters only

Solve it in your browserFree account, no card. Python, C++, Java or JavaScript, with hints if you get stuck, and your progress is saved.
Sign up free to solve