dsa · easy
Longest Palindrome
You may permute the letters of s. Return the length of the longest palindrome you can build this way. Letters are case-sensitive: "A" and "a" are different. You do not have to use every letter.
Arguments
s— bag of letters you may rearrange into a palindrome
A palindrome can use every even count in full, and at most one odd count as the center.
Example
s = "abccccdd" has d twice, c four times, and one each of a and b. One construction is "dccaccd" of length 7 (the leftover a/b cannot both sit in the center).
s = "a" → 1.
Constraints
1 <= s.length <= 2000 s contains only English letters (uppercase or lowercase). Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: 'abccccdd' Expected: 7
Example 2
Input: 'a' Expected: 1