#C7017. Balanced String Verification
Balanced String Verification
Balanced String Verification
You are given a string S and an integer T. A string is defined to be balanced if in every prefix of the string, the frequency of every character does not exceed the threshold T.
For example, consider S = "abac" with T = 2:
- Prefix "a": frequency of 'a' = 1 ≤ 2
- Prefix "ab": frequency of 'a' = 1, 'b' = 1 ≤ 2
- Prefix "aba": frequency of 'a' = 2, 'b' = 1 ≤ 2
- Prefix "abac": frequency of 'a' = 2, 'b' = 1, 'c' = 1 ≤ 2
This string is balanced so the result is YES
. If the threshold T is exceeded for any character in any prefix, then the string is not balanced and the answer is NO
.
You need to process multiple queries. For each query, output YES
if the string is balanced under the given threshold, otherwise output NO
.
Note: In your solution, all formulas should be in LaTeX format. For example, the condition that the frequency of each character does not exceed T can be written in LaTeX as \(\text{frequency} \le T\).
inputFormat
The input is read from standard input (stdin) and has the following format:
- An integer \(Q\) representing the number of queries.
- Each of the next \(Q\) lines contains an integer \(T\) and a string \(S\) separated by a space.
outputFormat
For every query, print a line containing either YES
if the string is balanced according to the given threshold \(T\), or NO
if it is not.
3
2 abac
1 aa
3 aabbbcc
YES
NO
YES
</p>