class Solution:
def longestPalindrome(self, s: str) -> int:
from collections import Counter
counts = Counter(s)
length = 0
odd_found = False
for count in counts.values():
if count % 2 == 0:
length += count
else:
length += count - 1
odd_found = True
if odd_found:
length += 1
return length
class Solution {
public:
int longestPalindrome(string s) {
unordered_map<char, int> counts;
for (char c : s) {
counts[c]++;
}
int length = 0;
bool odd_found = false;
for (auto it : counts) {
if (it.second % 2 == 0)
length += it.second;
else {
length += it.second - 1;
odd_found = true;
}
}
if (odd_found)
length += 1;
return length;
}
};
class Solution {
public int longestPalindrome(String s) {
int[] counts = new int[128];
for (char c : s.toCharArray()) {
counts[c]++;
}
int length = 0;
boolean oddFound = false;
for (int count : counts) {
if (count % 2 == 0) {
length += count;
} else {
length += count - 1;
oddFound = true;
}
}
if (oddFound) {
length += 1;
}
return length;
}
}
var longestPalindrome = function(s) {
let counts = {};
for (let c of s) {
counts[c] = (counts[c] || 0) + 1;
}
let length = 0;
let oddFound = false;
for (let key in counts) {
if (counts[key] % 2 === 0) {
length += counts[key];
} else {
length += counts[key] - 1;
oddFound = true;
}
}
if (oddFound) {
length += 1;
}
return length;
};
Given a string s
consisting of uppercase and lowercase letters, your task is to find the length of the longest palindrome that can be built using the letters from s
. Each letter in s
can be used at most as many times as it appears. You do not need to use all the characters, but you cannot reuse any character more than its frequency in s
.
The palindrome does not need to be a substring of s
; you can rearrange the letters to form it. The answer should be the maximum possible length of such a palindrome.
s
.
The first instinct might be to try all possible rearrangements of s
to see which one forms the longest palindrome, but that would be extremely inefficient for longer strings. Instead, we should think about the properties of palindromes and how character frequencies affect them.
In a palindrome, characters are mirrored around the center. This means that for every character, we need pairs (even counts) to place symmetrically. If a character appears an odd number of times, we can use all but one of them to form pairs, and possibly use a single odd character in the center.
The optimization comes from realizing that we just need to count how many pairs we can make from each character, and if there are any characters with odd counts, we can add one of them in the center for a longer palindrome.
s
.
count - 1
of them (which is even) for pairs. Keep track of whether any odd counts were found.
We use a hash map (or array for ASCII) for frequency counting because lookups and updates are O(1), making the whole process efficient.
Let's use the input s = "abccccdd"
.
So, the longest palindrome that can be built is of length 7 (e.g., "dccaccd").
s
. Summing the counts is O(1) if the character set is fixed (e.g., ASCII), or O(k) where k is the number of unique characters.
The key insight is that palindromes are built from pairs of characters, and at most one unpaired character can be placed in the center. By counting character frequencies and using as many pairs as possible, we can efficiently compute the maximum palindrome length. This approach is both simple and elegant, making use of basic data structures and properties of palindromes for optimal performance.