#K44952. Longest Palindrome Construction
Longest Palindrome Construction
Longest Palindrome Construction
You are given a string s
of length n
consisting of lowercase English letters. Your task is to determine the maximum possible length of a palindrome that can be constructed using the characters of the string s
. A palindrome is a string that reads the same forwards and backwards.
More formally, let the frequency of each character in the string be denoted by \( f(c) \). The length of the longest palindrome that can be formed is given by:
[ L = \sum_{c} \left( f(c) - (f(c) \bmod 2) \right) + \begin{cases} 1, & \text{if any } f(c) \text{ is odd} \ 0, & \text{otherwise} \end{cases} ]
For example:
- For
s = "abccccdd"
withn = 8
, the answer is7
. - For
s = "aabbc"
withn = 5
, the answer is5
. - For
s = "abc"
withn = 3
, the answer is1
.
Build your solution so that it reads input from standard input (stdin) and writes the result to standard output (stdout).
inputFormat
The input is read from standard input and consists of two lines. The first line contains an integer ( n ) (the length of the string). The second line contains the string ( s ) consisting of lowercase English letters.
outputFormat
Output a single integer representing the length of the longest palindrome that can be constructed using the characters of ( s ). The output should be written to standard output.## sample
7
abccccdd
7