Happy Ladybugs is a board game having the following properties:
- The board is represented by a string, b, of length n. The ith character of the string, b[i], denotes the ith cell of the board.
- If b[i] is an underscore (i.e., _), it means the ith cell of the board is empty.
- If b[i] is an uppercase English alphabetic letter (ascii[A-Z]), it means the ith cell contains a ladybug of color b[i].
- String b will not contain any other characters.
- A ladybug is happy only when its left or right adjacent cell (i.e., ) is occupied by another ladybug having the same color.
In a single move, you can move a ladybug from its current position to any empty cell.
Given the values of and for games of Happy Ladybugs, determine if it’s possible to make all the ladybugs happy. For each game, returnYES
if all the ladybugs can be made happy through some number of moves. Otherwise, returnNO
.
Example
b=[YYR_B_BR]
You can move the rightmost B and R to make b=[YYRRBB__BR__] and all the ladybugs are happy. Return YES
.
Function Description
Complete the happyLadybugs function in the editor below.
happyLadybugs has the following parameters:
- string b: the initial positions and colors of the ladybugs
Returns
- string: either
YES
orNO
Input Format
The first line contains an integer g, the number of games.
The next g pairs of lines are in the following format:
The first line contains an integer n, the number of cells on the board.
The second line contains a string b that describes the n cells of the board.
Constraints
- 1 <= g,n <= 100
Sample Input 0
1 | 4 |
Sample Output 0
1 | YES |
Explanation 0
The four games of Happy Ladybugs are explained below:
Initial board:
After the first move:
After the second move:
After the third move:
Now all the ladybugs are happy, so we print
YES
on a new line.There is no way to make the ladybug having color Y happy, so we print NO on a new line.
There are no unhappy ladybugs, so we print YES on a new line.
Move the rightmost B and R to form b=[BBRRR__].
Sample Input 1
1 | 5 |
Sample Output 1
1 | NO |
Solution
1 | /* |