#C5839. Transform String to Palindrome
Transform String to Palindrome
Transform String to Palindrome
You are given a string s
and an integer k
. Your task is to determine whether the string s
can be transformed into a palindrome by changing at most k
characters.
A string is a palindrome if it reads the same backward as forward. For example, "racecar" is a palindrome while "hello" is not.
The transformation is measured by counting the number of mismatches between characters at symmetric positions. Formally, if you denote the length of the string as n, for every index i (where 0 ≤ i < n/2
), if s[i] != s[n-i-1]
, one change is needed. If the total number of mismatches is less than or equal to k
, then the transformation to a palindrome is possible.
You need to process multiple test cases. For each case, output "YES" if the transformation is possible, otherwise output "NO".
The answer should be computed using LaTeX formatted formulas when applicable. For example, the condition can be written as \(\text{mismatches} \leq k\).
inputFormat
The first line contains an integer t
representing the number of test cases. Each of the following t
lines contains a string s
and an integer k
separated by a space.
Input Format:
t s1 k1 s2 k2 ... st k
outputFormat
For each test case, output a single line containing either "YES" or "NO" depending on whether it is possible to transform the string into a palindrome by changing at most k
characters.
Output Format:
result1 result2 ... resultt## sample
3
abca 1
abcd 1
abcde 2
YES
NO
YES
</p>