Professor And Directions

Professor And Directions

CodeChef Problem Code: DIRECTN

The Professor is facing the North. Tokyo is in trouble, and she is facing the South. Professor being her guardian angel wants to help her. So, The Professor will follow some instructions, given as a string S of length N, and will turn either left or right according to these instructions. He can save Tokyo only if after following a substring of instructions he will face in the same direction that Tokyo is facing. Will the Professor be able to save Tokyo?

Input Format
The first line contains an integer T denoting the number of test cases. The T test cases then follow. The first line of each test case contains N.
The second line contains a string that contains only 'L' and 'R', where 'L' represents left and 'R' represents right.

Output Format
For each test case, output "YES" if the Professor will be able to save Tokyo and "NO" otherwise. Output is case insensitive, which means that "yes", "Yes", "YEs", "no", "nO" - all such strings will be acceptable.

Sample Input 1

3
12
LRLRRRLRLLLL
2
LR
4
LRRL

Sample Output 1

YES
NO
YES

Explanation
Test case 1: Professor can select the substring "RLRLLL", after following the instructions given by it, Professor will face South.

Test case 2: No matter which substring Professor selects, the final direction of Professor will never be that in which Tokyo is facing.

Solution

To run the code paste Sample Input in Stdin Inputs.