#K74797. Rearrange to Palindrome
Rearrange to Palindrome
Rearrange to Palindrome
You are given a string s of length n and a non-negative integer k. Your task is to determine whether it is possible to rearrange the characters of s into a palindrome by performing at most k rearrangements. In other words, if the number of character pairs that must be rearranged to form a palindrome is at most k, print YES
; otherwise, print NO
.
The key observation is that a string can be rearranged into a palindrome if at most one character has an odd frequency count. More formally, if we let odd_count denote the number of characters with an odd frequency, then the condition is satisfied if and only if
[ \left\lfloor \frac{odd_count}{2} \right\rfloor \leq k ]
Note: The input is provided via stdin and output should be printed to stdout.
inputFormat
The input consists of two lines:
- The first line contains two integers
n
andk
wheren
is the length of strings
andk
is the maximum number of allowed rearrangements. - The second line contains the string
s
consisting only of lowercase English letters.
outputFormat
Output a single line containing either YES
if it is possible to rearrange the string into a palindrome with at most k
rearrangements, or NO
otherwise.
6 2
aababc
YES