#C2356. Reduce String to Empty
Reduce String to Empty
Reduce String to Empty
You are given a string s of length n. In one operation, you can remove any two adjacent characters if they are the same. Formally, you can choose an index i (1 ≤ i < n) such that \(s_i = s_{i+1}\) and remove both characters from the string, concatenating the remaining parts. You can perform this operation any number of times. Your task is to determine whether it is possible to reduce the string to an empty string by applying the operation repeatedly.
Example:
- For s = "abccba" (n = 6), you can remove "cc" to get "abba", then remove "bb" to get "aa", and finally remove "aa" to obtain an empty string. The answer is
YES
. - For s = "aabbc" (n = 5), it is impossible to reduce it completely. The answer is
NO
.
Note: The operation is only allowed when the two adjacent characters are identical.
inputFormat
The first line of input contains an integer \(T\) (\(T \ge 1\)), representing the number of test cases.
Each test case is represented by two lines:
- The first line contains an integer \(n\), the length of the string.
- The second line contains the string \(s\) of length \(n\) consisting of lowercase English letters.
outputFormat
For each test case, output a single line containing YES
if it is possible to reduce the string to an empty string entirely using the allowed operation, or NO
otherwise.
5
6
abccba
5
aabbc
4
aaaa
1
a
2
aa
YES
NO
YES
NO
YES
</p>