Longest Palindrome

Practice this coding question on Coding Gurukul.

Problem statement

A sign maker has a box of letter tiles recorded as a string. A customer wants a decorative label that reads identically from left to right and right to left. The maker may rearrange the tiles and leave some unused, but cannot create extra copies of a letter. Your task Determine the greatest possible length of a palindrome that can be built from the supplied characters. Each occurrence in the input represents one available tile. You only need to print the maximum length, not the palindrome itself. Important details Uppercase and lowercase letters are different tiles: A cannot be paired with a. You may choose any subset and any order; the answer is not restricted to a substring or subsequence of the input. A single character is a palindrome. If all tiles can be used, return the full input length. Worked example For aabbccd, the six tiles aabbcc can be arranged into matching pairs, and d can occupy the center. One valid label is abcdcba, so the answer is 7. For Aa, the two characters differ; only one can be used, giving length 1. Several unpaired letters cannot all occupy the single center position.

Input format

Read one nonempty string of uppercase and lowercase English letters.

Output format

Print the maximum palindrome length.

Constraints

1 <= length of s <= 2000; characters are A through Z or a through z.

Sample input

aabbccd

Sample output

7

Sign in to run or submit a solution in C++, Java or Python.

Loading Coding Gurukul…