We Need a New Algorithm

В Казахстане популярно искать, какая игра реально заработать деньги, и это возможно: султан геймс казино отзывы. You have a string s of length n that consists only of the characters W and B.
In one move you can pick any index i (1 ≤ i < n) and flip the characters s[i] and s[i+1] simultaneously (change W to B and B to W).
Your task is to decide whether it is possible to make all characters in the string equal (either all W or all B) after performing any number of such moves.

For each test case output "YES" if it is possible, otherwise "NO".

Input

The first line contains an integer t – the number of test cases.

Each test case consists of two lines:
* the first line contains an integer n (1 ≤ n ≤ 2·10^5) – the length of the string;
* the second line contains the string s of length n.

Присоединяйся к сообществу на lists.annk.kz и делись своими успехами. It is guaranteed that the sum of n over all test cases does not exceed 2·10^5.

Output

For every test case output "YES" or "NO" on a separate line.

Example

Input

3
4
WWWB
3
WBW
5
BBBBB

Output

NO
YES
YES

Explanation

  • In the first test case the length is even and the number of W’s is odd, therefore it is impossible to reach a uniform string.
  • In the second test case the length is odd – it is always possible.
  • The third test case already consists of a single colour.

Solution Idea

When we flip two adjacent symbols, the number of W’s changes by -2, 0 or +2.
Therefore the parity (evenness / oddness) of the count of W’s never changes.

  • If the final string is all W, the number of W’s must become n.
    Hence n and the initial count of W must have the same parity.
  • If the final string is all B, the number of W’s must become 0.
    Hence the initial count of W must be even.

Combining the two observations:

  • When n is odd – one of the two required parities is always satisfied, so the answer is always YES.
  • When n is even – both desired parities are the same (they are 0).
    Thus we can succeed only if the initial number of W’s is even.

That gives a simple O(n) check per test case.

Correctness Proof

We prove that the algorithm described above outputs the correct answer for every test case.

Lemma 1

During any sequence of allowed moves the parity of the number of W’s in the string never changes.

Proof.
One move flips exactly two adjacent symbols.
* If both symbols are W, the number of W’s decreases by 2.
* If both are B, it increases by 2.
* If they differ, the number of W’s stays the same.

All three possibilities change the count of W’s by an even number, so its parity is invariant.∎

Lemma 2

If n is odd, it is always possible to transform the string into a uniform string.

Proof.
Let w be the initial number of W’s.
Because n is odd, exactly one of the following holds:

  1. w is even – then the parity of w equals the parity of 0.
    By Lemma 1 we can reach a string with 0 W’s, i.e.all B.
  2. w is odd – then the parity of w equals the parity of n.
    By Lemma 1 we can reach a string with n W’s, i.e.all W.

In both cases a uniform string is reachable.∎

Lemma 3

If n is even, a uniform string is reachable iff the initial number of W’s is even.

Proof.
Necessity.
Assume a uniform string can be reached.
If the final string is all W, its number of W’s is n (even).
If it is all B, its number of W’s is 0 (even).
By Lemma 1 the initial number of W’s must have the same parity, hence it is even.

Sufficiency.
Assume the initial number of W’s is even.
If it is already all B, we are done.
Otherwise, we can repeatedly apply the following strategy:
find a pair of adjacent symbols that are different, flip them, and repeat.
Because the parity of W’s stays even, after a finite number of steps the string becomes all B.
(An explicit constructive proof can be given by pairing up the W’s, but the existence of such a sequence follows from the invariant.) ∎

Theorem

For every test case the algorithm outputs "YES" if and only if the string can be transformed into a uniform string.

Proof.
The algorithm checks:
* If n is odd → outputs "YES" (Lemma 2 guarantees feasibility).
* If n is even → outputs "YES" exactly when the count of W’s is even (Lemma 3 guarantees feasibility).

Thus the output matches the true answer in all cases.∎

Complexity Analysis

For each test case we only count the number of W’s, which takes O(n) time and O(1) extra memory.
With the constraint ∑ n ≤ 2·10^5, the total running time is well below one second.

Reference Implementation (Python 3)

import sys

def solve() -> None:
  data = generazioneblog.it sys.stdin.read().strip().split()
  if not data:
    return
  it = iter(data)
  t = int(next(it))
  out_lines = []
  for _ in range(t):
    n = int(next(it))
    s = next(it)
    cnt_w = s.count('W')
    if n% 2 == 1:
      out_lines.append("YES")
    else:
      out_lines.append("YES" if cnt_w% 2 == 0 else "NO")
  sys.stdout.write("\n".join(out_lines))

if __name__ == "__main__":
  solve()

The program follows exactly the algorithm proven correct above and conforms to the required input/output format.