This page is still under construction.

Parts of this page are still being built. What you see may change.

Three Bit Computer

Time limit1sMemory limit32 MB

Summary
Given a string over {a,b,c}, decide whether an all-uninitialized memory can be initialized to exactly that string with the two given operations.
Level

Medium5 of 10

Topics
Dynamic programming, Greedy
Solved
No attempts yet

Problem

The scientists of the Kingdom of Byteland are building a new machine called the Three Bit Computer (TBC). Before it can run, they must solve a problem about how its memory is initialized, and they have asked for your help.

The current TBC has nn memory cells, numbered from 11 to nn. Every cell is either uninitialized or holds one of the three values aa, bb, or cc. The machine supports exactly two initialization operations:

  • Take two consecutive cells ii and i+1i+1 (with 1≤i<n1 \le i < n) that are both uninitialized, and set them to two different values.
  • Take two consecutive cells where one is uninitialized and the other already holds a value xx, and set both of them to the two values different from xx (that is, the two values of {a,b,c}∖{x}\{a, b, c\} \setminus \{x\}, in either order).

For example, with n=4n = 4 the following initialization is possible (writing uu for an uninitialized cell):

uuuu→uuab→ucbb→babbuuuu \rightarrow uuab \rightarrow ucbb \rightarrow babb

Given a target pattern that every cell must end up holding, write a program that decides whether the memory can be initialized to that exact pattern, starting from a completely uninitialized memory.

Input

The first line contains a single integer NN (1≤N≤101 \le N \le 10), the number of target patterns.

Each pattern is described on two lines:

  • the first line contains an integer ℓi\ell_i (1≤ℓi≤100 0001 \le \ell_i \le 100\,000), the number of memory cells for that pattern;
  • the second line contains a string of length ℓi\ell_i consisting only of the letters aa, bb, and cc — the target pattern itself.

Output

Print NN lines, one per pattern in the given order. For the ii-th pattern print YES if the memory can be initialized to that pattern, or NO otherwise.

Examples4

  1. Example 1

    Input
    2
    4
    aabb
    4
    aaab
    
    Expected output
    NO
    YES
    
  2. Example 2

    Input
    1
    1
    a
    
    Expected output
    NO
    
  3. Example 3

    Input
    6
    2
    ab
    2
    ba
    2
    aa
    2
    cc
    2
    bc
    2
    ca
    
    Expected output
    YES
    YES
    NO
    NO
    YES
    YES
    
  4. Example 4

    Input
    8
    3
    abc
    3
    cba
    3
    aaa
    3
    bbb
    3
    aab
    3
    abb
    3
    bba
    3
    aba
    
    Expected output
    NO
    NO
    NO
    NO
    YES
    YES
    YES
    YES