You can rearrange the letters freely — so positions stop mattering and counts take over. It becomes pure bookkeeping: how many pairs, plus one center.
Given a string of upper- and lowercase letters, you may reorder the characters however you like. Return the
maximum length of a palindrome you could build — you never need to print the palindrome itself. For
bbde (b:2, d:1, e:1), use the pair bb and drop one odd letter in the
middle: bdb, length 3.
Because order is yours to choose, bbde, edbb, and bdeb all give the same
answer — only the frequencies matter. That kills the tempting brute forces: generating permutations is O(N!),
trying every subset is O(2ᴺ), and both solve a harder problem than asked. You just need pair arithmetic.
For a character appearing f times, the most you can use symmetrically is the largest even number ≤
f, i.e. (f / 2) * 2. Sum those across all characters. Then, if any character
had an odd count, one leftover can take the single center seat — add 1.
a:3, b:3, c:3 you can't use all 9 — a palindrome has exactly one center. You get
2+2+2 + 1 = 7, not 9. Extra odd leftovers are simply discarded.
You can only wear socks in pairs. Count how many complete pairs each color makes; odd ones leave a single sock behind — and you're allowed to display just one lone sock, in the middle.
Whatever you place on the left must be reflected on the right. Only the letter sitting exactly on the mirror line escapes needing a twin.
Each letter is a stock of parts. Every symmetric slot costs 2 identical parts. Spend as many full pairs as you can afford, then treat yourself to one single-part centerpiece.
Type a string (or pick one). Each letter's count splits into pair blocks and a possible leftover. The stage assembles an actual palindrome: pairs mirror across the dashed center line, and one leftover (if any) drops into the middle in blue.
Try aaabbbccc (three odds → only one center used),
AaBb (case matters, all singletons → length 1), or aabbcc (all even → use everything).
int[128]
keeps them in separate slots automatically.static int longestPalindrome(String s) { int[] freq = new int[128]; // covers A-Z and a-z separately for (char ch : s.toCharArray()) freq[ch]++; int length = 0; boolean hasOdd = false; for (int c : freq) { length += (c / 2) * 2; // largest even ≤ c if (c % 2 == 1) hasOdd = true; } if (hasOdd) length++; // one center seat return length; }
Counting is O(N); scanning the fixed 128-slot table is constant. So it's O(N) time, O(1) space
— optimal, since you must read every character once. An equivalent one-liner formula: with oddCount
odd-frequency letters, the answer is s.length() − oddCount + 1 when any odd exists, else
s.length(). The pair-based version is easier to reconstruct from first principles.
| Input | Frequencies | Pairs used | Center? | Answer |
|---|---|---|---|---|
bbde |
b:2, d:1, e:1 | 2 | +1 | 3 |
aabbcc |
a:2, b:2, c:2 | 6 | — | 6 |
abcd |
all 1 | 0 | +1 | 1 |
aaabbbccc |
a:3, b:3, c:3 | 6 | +1 | 7 |
AaBb |
A,a,B,b : 1 | 0 | +1 | 1 |
The answer isn't just an upper bound — it's constructible. Lay each used pair symmetrically
(aabc … cbaa) and slot one leftover between the halves; that string is automatically a palindrome.
This is the third distinct question answered by frequency counting in this set. The tool is identical; what changes is the condition you read off the counts: