Build the longest palindrome length possible from the letters of a given string.
Problem
Given a string s consisting of lowercase and/or uppercase English letters, determine the maximum length of a palindrome that can be built using the characters of s.
You may rearrange the characters in any order, but each character can be used at most as many times as it appears in the input.
A palindrome reads the same forward and backward.
Goal
Return the length of the longest palindrome that can be formed.
Input Format
- A single string
s. - The string contains only alphabetic characters.
Output Format
- Return one integer: the maximum possible length of a palindrome that can be constructed from the characters of
s.
Constraints
1 <= s.length.- Characters may be treated as case-sensitive if present.
- Use each character no more times than it appears in
s.
Example 1
Input
s = "abccccdd"
Output
7
Explanation
One longest palindrome is "dccaccd". It uses 2 d's, 4 c's, and 1 a or b as the center, giving length 7.
Example 2
Input
s = "a"
Output
1
Explanation
A single character is already a palindrome.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.