#K76867. Palindrome Formation from Character Frequencies
Palindrome Formation from Character Frequencies
Palindrome Formation from Character Frequencies
Given a set of characters and their frequencies, determine whether it is possible to rearrange them to form a palindrome.
A string is a palindrome if it reads the same forwards and backwards. In order for a set of characters to form a palindrome, at most one character can have an odd frequency. Mathematically, if we define \(oddCount\) as the number of characters whose frequency is odd, then the condition to form a palindrome is:
\(oddCount \le 1\)
You are given the number of distinct characters followed by each character and its frequency. Output True
if a palindromic permutation exists, and False
otherwise.
inputFormat
The input is given via stdin and consists of:
- An integer
N
representing the number of distinct characters. N
lines, each containing a character and an integer frequency, separated by a space.
For example:
3 a 2 b 3 c 2
outputFormat
Print a single line to stdout containing True
if it is possible to form a palindromic string from the given frequencies, or False
otherwise.
3
a 2
b 2
c 2
True
</p>