Menu
CoddyTech

Longest Palindrome

You get a string s of upper and lower case letters. Rearrange its letters to build the longest palindrome you can and return that palindrome's length. You may put the letters in any order and leave some out, but you can use each letter at most as many times as it appears in s.

Upper and lower case are different letters, so Aa is not a palindrome.

Function

longestPalindrome(s: string) → integer
sstring
the letters you may use, upper and lower case
Returnsinteger
the length of the longest palindrome you can build from those letters

Constraints

  • 1 ≤ s.length ≤ 5 × 104
  • s contains only English letters, a to z and A to Z.
  • You do not have to use every letter.

Examples

Input
s = "coddyCode"
Output
5
Explanation
o appears twice and d three times, the rest once. A pair of os, a pair of ds and one leftover letter in the middle give length 5, for example doCod.
challenge icon

Follow-up

Can you return one longest palindrome itself, along with its length, still in O(n) time?

Reset code
def longestPalindrome(s):
    # Write code here

Test #1

Test #2

Test #3

Argument

s = "coddyCode"

Expected Output

5