Professor And Directions solution codechef

Professor And Directions solution codechef

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 SS of length NN, 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?

Professor And Directions solution codechef

Input Format

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

Output Format

Professor And Directions solution codechef

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.

Constraints

  • 1T1001≤T≤100
  • 2N1052≤N≤105
  • The string SS consists of uppercase characters 'L' and 'R' only.
  • The sum of NN over all test cases does not exceed 106106.

Sample Input 1

Professor And Directions solution codechef

3
12
LRLRRRLRLLLL
2
LR
4
LRRL

Sample Output 1

Professor And Directions solution codechef

YES
NO
YES

Explanation

Professor And Directions solution codechef

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

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

Leave a Comment

Your email address will not be published. Required fields are marked *