#C9485. Longest Palindrome Reordering
Longest Palindrome Reordering
Longest Palindrome Reordering
You are given a string s
. Your task is to determine the length of the longest palindrome that can be built using the characters of s
. In other words, you need to calculate the maximum length of a palindrome that can be formed from the letters in s
by rearranging them.
The idea is to count how many characters appear an even number of times, and for those with odd counts, use the maximum even number less than the count (i.e. count - (count mod 2)
). If there exists at least one character with an odd frequency, you can place exactly one odd character in the middle of the palindrome. Mathematically, the maximum length can be expressed as:
$$\text{maxLength} = \sum_{i}(count_i - (count_i \bmod 2)) + \begin{cases}1, & \text{if any } count_i \text{ is odd} \\ 0, & \text{otherwise}\end{cases}$$
This problem is a classic application of greedy counting.
inputFormat
The input consists of a single line containing the string s
. The string may consist of lowercase, uppercase, or other ASCII characters.
Input is provided via standard input (stdin).
outputFormat
Output a single integer representing the length of the longest palindrome that can be built with the given string.
Output should be written to standard output (stdout).
## sampleabccccdd
7