#C7012. String Formation Operations
String Formation Operations
String Formation Operations
You are given a number of operations and a target string. In each test case, you are given three parameters:
- N: the total number of operations allowed,
- K: the maximum allowed occurrences of each letter ('a' and 'b'),
- Q: the target string composed only of the characters 'a' and 'b'.
Your task is to determine if it is possible to form the string Q
after performing exactly N
operations. It is guaranteed that each single operation appends one character. However, if there are any extra operations beyond the length of Q
, they must be performed in pairs (for example, an append and a corresponding removal that leaves the string unchanged). Therefore, the condition on the extra operations is that N - |Q|
must be even. In addition, the counts of 'a' and 'b' in the target string must not exceed K
respectively.
Formally, given a test case with parameters \( N \), \( K \), and \( Q \), the answer is "YES" if and only if \[ \text{count}_{a}(Q) \le K, \quad \text{count}_{b}(Q) \le K, \quad N \ge |Q|, \quad \text{and} \quad (N - |Q|) \equiv 0 \pmod{2}. \] Otherwise, the answer is "NO".
inputFormat
The first line contains a single integer T
indicating the number of test cases. Each of the following T
lines contains a test case in the following format:
N K Q
Where:
N
is the total number of operations allowed,K
is the maximum allowed occurrence for each of 'a' and 'b',Q
is the target string consisting only of the characters 'a' and 'b'.
outputFormat
For each test case, output a single line with either "YES" or "NO". "YES" if it is possible to form the string Q
under the given conditions; otherwise, "NO".
4
5 3 abb
4 4 bb
6 2 aaab
7 2 abba
YES
YES
NO
NO
</p>