#C3117. Forming a Nearly Palindrome
Forming a Nearly Palindrome
Forming a Nearly Palindrome
You are given a string s of length n and an integer m. Your task is to determine whether it is possible to rearrange the characters of s and remove at most m characters so that the resulting string becomes a palindrome.
A palindrome is a string that reads the same forward and backward. In order to form a palindrome, at most one character can appear an odd number of times. Formally, if we denote the number of characters that have an odd frequency as odd_count, then the minimum number of removals needed to form a palindrome is given by:
If this minimum number of removals is less than or equal to m, then it is possible to form a palindrome, and you should output YES; otherwise, output NO.
Note: You are allowed to rearrange the string arbitrarily before making removals.
inputFormat
The first line contains an integer T representing the number of test cases. Each test case consists of a single line containing an integer n (the length of the string), a string s of length n, and an integer m, separated by spaces.
For example:
4 5 abcbc 1 3 aaab 1 6 racecar 0 4 abcda 1
outputFormat
For each test case, output a single line containing either YES if it is possible to rearrange the string and remove at most m characters to form a palindrome, or NO otherwise.
4
5 abcbc 1
3 aaab 1
6 racecar 0
4 abcda 1
YES
YES
YES
NO
</p>