professor and directions

 Firstly, this problem was not at all the level in which this is posted, I took around 6 mins to implement this including the ideation part too, and I was a bit confused about the corner cases, cause I couldn't think of any. After about 2 mins of thinking and also considering if any problem may occur due to the constraints, I couldn't come up with any possible corner case, and it was because there wasn't any, I was just thinking that how can this be so direct, but afterwards I got this that maybe they wanted to confuse people by making them think about how to check whether the Professor will face the South or not, but mostly we have to think in a different angle in problems, if you wanna go right then go left, that's how it goes in this world. Most of the times the confusing problems have just a single part where we can break it to get to the core of the problem.

Well this what somewhat similar to the Mountain Hiking Problem that I had seen before( maybe that was last summer ).

                                                                                        Some SE Stuff... :)


The problem statement should be well cleared before going to the Code part or you won't get a word.

Problem Statement:-

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  of length , 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  denoting the number of test cases. The  test cases then follow.
  • The first line of each test case contains .
  • 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.

Constraints

  • 1100
  • 2105
  • The string  consists of uppercase characters 'L' and 'R' only.
  • The sum of  over all test cases does not exceed 106.

Sample 1:

Input
Output
3
12
LRLRRRLRLLLL
2
LR
4
LRRL
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.

Code Implementation:-

#include<bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false);
using namespace std;

int main() {
    fast
    int t;
    cin>>t;
    while(t){
        int n;
        cin>>n;
        string s;
        cin>>s;
       
        int chk=0;
        for(int i=0;i<n-1;i++){
            if(s[i]==s[i+1]){
                cout<<"YES"<<endl;
                chk=1;
                break;
            }
        }
        if(chk==0){
            cout<<"NO"<<endl;
        }
       
        t--;
    }
    return 0;
}

Explanation:-

Simply, we have to find whether Professor will ever see in the South Direction or not. And as currently he is facing the North direction so it is very obvious logic that he will require either 2 R or 2 L to face the South Direction, and that's all we gotta find in the string. So we just need to traverse the string(from 1st elem till the 2nd last) and if we get any of the character same as the proceeding elem then we can understand or conclude that the Professor can face the South direction, and about the substring part, that all is just to confuse people by throwing jargons, well substring is common but mostly people confuse them with subsequences, but that's not at all required in this context, as the logic is super simple if we get 2 chars same then possible else not. That's it!

Comments

Popular Posts