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.
- length
- 0
Order doesn't matter, so count: a 1, b 1, c 4, d 2.
You may rearrange the letters. What decides how long a palindrome you can build?
- AHow many of each letter you have
- BThe order the letters come in
- 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.
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.
s = "abccccdd" → 7
0 ≤ length ≤ 100,000 · letters only