Skills101
← All coding problems

Longest Palindrome from Letter Tiles

easyStrings II+10 XPTime limit 2s

You have a bag of letter tiles (written as a string). Using some of the tiles, in any order, build the longest possible palindrome (a word that reads the same forwards and backwards). Upper and lower case are different tiles: A and a don't pair up.

From abccccdd you can build dccaccd — length 7.

Input: one line with the tiles (upper and lower case English letters).

Output: the length of the longest palindrome you can build.

Constraints: 1 ≤ length ≤ 10⁵.

Think about it: a palindrome is made of matching pairs placed symmetrically, plus at most one unpaired tile in the very middle. Count each letter: use count / 2 pairs of it, and add 1 at the end if any letter had an odd count.

Sample input 1

abccccdd

Sample output 1

7

Sample input 2

Aa

Sample output 2

1

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.