We're not asked to find a palindrome within the string. We're asked to build the longest possible palindrome using the characters available in the string. We can rearrange characters however we want, and we don't have to use all of them.
What makes a string a palindrome? It reads the same forwards and backwards. That means every character must appear in pairs, one for each side. The only exception is the middle character: if the palindrome has an odd length, exactly one character can appear an odd number of times (it sits in the center).
So the real question is how many characters we can use. Every character that appears an even number of times can be fully used. For characters that appear an odd number of times, we can use all but one of them (the even portion), and then place one leftover character in the middle. The length follows directly from the character counts.
1 <= s.length <= 2000 → A single pass over the string is enough; the answer depends only on character counts, so an O(n) solution is the natural target.s consists of lowercase and/or uppercase English letters only → At most 52 distinct characters (26 lowercase + 26 uppercase). The frequency map has a fixed, small size, which makes the per-character bookkeeping constant space.We don't need to build the palindrome, only count how many characters can go into one.
A palindrome has a mirror structure: every character in the left half has a matching character in the right half. A character that appears an even number of times contributes all its copies. A character that appears an odd number of times contributes count - 1 copies (the largest even number below count), leaving one copy unused.
There is exactly one center position, so at most one unused copy can be placed in the middle. We sum the even contributions across all characters, then add 1 if any character had an odd count.
Integer division gives count / 2 * 2 as the largest even number not exceeding count (5 yields 4, 4 yields 4). Taking the even portion of every count is safe to do independently because pairs from different characters never compete: each pair occupies one symmetric slot on the left and its mirror on the right. The only shared resource is the single center slot, and the +1 claims it once if any odd count exists. No arrangement can do better, since the two halves must be identical multisets and that caps the usable count of each character at its even portion.
length = 0 and hasOdd = false.freq / 2 * 2 to length (the largest even number <= freq). This is the even portion.freq is odd, set hasOdd = true.hasOdd is true, add 1 to length (for the center character).length.This approach counts every character first, then processes the counts in a second pass over the frequency array. The next approach computes the same answer in a single pass over the string, tracking parity directly instead of storing full counts.
Instead of counting frequencies and then analyzing them, we can use a set to track characters with odd counts as we go. When we see a character, if it's already in the set (meaning we've seen it an odd number of times), we remove it and add 2 to the length (a pair is complete). If it's not in the set, we add it, marking that we've seen it an odd number of times so far.
At the end, if the set is non-empty, there's at least one character with an odd count, so we can add 1 for the center.
The set holds exactly the characters seen an odd number of times so far, since each occurrence toggles membership. A character toggles out on its even occurrences, and each toggle-out completes one pair and adds 2. After the loop, a character remains in the set precisely when its total count is odd, so the set size equals the number of odd-count characters. This matches Approach 1: the sum of pairs times 2 is the total even contribution, and a non-empty set is the same condition as hasOdd.
length = 0.length (a pair is formed).length (center character).length.