#C3270. Longest Palindrome Length
Longest Palindrome Length
Longest Palindrome Length
Problem Description:
You are given a string (s) consisting of lowercase letters. Your task is to determine whether the characters of (s) can be rearranged to form a palindrome, and if so, compute the length of the longest palindrome that can be constructed using these characters.
A palindrome is a sequence of characters that reads the same forward and backward. The length of the longest palindrome is computed as follows:
[
\text{length} = \sum\limits_{\text{char in } s} \Big( count(\text{char}) - (count(\text{char}) \bmod 2) \Big) + \begin{cases} 1, & \text{if there exists at least one odd count}\ 0, & \text{otherwise} \end{cases}
]
However, if the resulting length is exactly 1 for a string with more than one character (i.e. (|s| > 1)), then it is deemed impossible to form a valid palindrome and you should output (-1).
For example:
- For (s = \texttt{abccccdd}), a palindrome of length (7) (such as (\texttt{dccaccd})) can be formed.
- For (s = \texttt{abcdefg}), no palindrome of length greater than (1) can be formed, so the output is (-1).
inputFormat
Input Format:
The input consists of a single line containing a non-empty string (s) of lowercase letters.
outputFormat
Output Format:
Output a single integer representing the length of the longest palindrome that can be formed using the characters of (s). If the longest palindrome has a length of (1) while (|s| > 1), output (-1).## sample
abccccdd
7
</p>