#K55422. Palindrome Transformation with Exactly K Reversals
Palindrome Transformation with Exactly K Reversals
Palindrome Transformation with Exactly K Reversals
You are given a string S and an integer K. You need to determine if it is possible to transform the string into a palindrome by performing exactly K reversal operations on its substrings. A reversal operation means choosing any substring of S and reversing its characters.
There are two cases to consider:
- If S is already a palindrome, then performing an even number of reversals will keep it a palindrome. However, an odd number of reversals will spoil the symmetry.
- If S is not a palindrome, then you must perform enough reversals to fix the mismatches. In this problem, it is guaranteed that if K is greater than or equal to the length of S (i.e. $$K \geq |S|$$), then the transformation is always possible.
Your task is to output "Yes" if the transformation is possible using exactly K reversals, and "No" otherwise.
inputFormat
The first line contains an integer T denoting the number of test cases. Each of the following T lines contains a string S and an integer K separated by a space.
outputFormat
For each test case, output a single line with the answer "Yes" if the transformation is possible with exactly K reversals, or "No" otherwise.
## sample5
abcba 2
abccba 1
racecar 1
abcdefg 0
abc 3
Yes
No
No
No
Yes
</p>