DEV Community

Cover image for Codechef - Professor And Directions
Atharva Siddhabhatti
Atharva Siddhabhatti

Posted on

Codechef - Professor And Directions

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

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

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.

Explanation

The Professor he first Facing in the North side goal is to save Tokyo, Tokyo will be saved only if professor is facing south after following instructions. The instructions contains letter 'L' for Left and 'R' for Right. We have defined two function namely twoLeft and twoRight which is checking if the L or R letter consecutively appered in the string for 2 or more than two times. If that is the case rotating in same direction twice will lead to be facing the opposite direction the person is standing in this case the professor was standing in North Direction.

After getting the number of consecutive 'L' or 'R' . The next condition checks if the count is 2 or greater than 2 if it is true than YES is printed otherwise No will be printed and Professor will not be able to save Tokyo.

Condtion to check if 'L' or 'R' count is equal to or greater than 2.

            int max = twoRight(str);
            int max1 = twoLeft(str);
            if(max >= 2 || max1 >=2) {
                System.out.println("YES");
            }else {

                System.out.println("NO");
            }
Enter fullscreen mode Exit fullscreen mode

Check number of lefts and number or rights

private static int twoRight(String str) {
        int max = 0;
        for (int i = 0; i < str.length(); i++) {
            int count = 0;
            for (int j = i; j < str.length(); j++) {
                if (str.charAt(i) == str.charAt(j)) {
                    if(str.charAt(i)=='R'){
                        count++;
                    }

                } else break;
            }
            if (count > max) max = count;
        }
        return max;
        }
    private static int twoLeft(String str) {
        int max1 = 0;
        for (int i = 0; i < str.length(); i++) {
            int count = 0;
            for (int j = i; j < str.length(); j++) {
                if (str.charAt(i) == str.charAt(j)) {
                    if(str.charAt(i)=='L'){
                        count++;
                    }

                } else break;
            }
            if (count > max1) max1 = count;
        }
        return max1;
        }
Enter fullscreen mode Exit fullscreen mode
 int max = twoRight(str);
            int max1 = twoLeft(str);
            if(max >= 2 || max1 >=2) {
                System.out.println("YES");
            }else {

                System.out.println("NO");
            }
Enter fullscreen mode Exit fullscreen mode

Input:

3
12
LRLRRRLRLLLL
2
LR
4
LRRL
Enter fullscreen mode Exit fullscreen mode

Output:

YES
NO
YES

Enter fullscreen mode Exit fullscreen mode

Link to original problem https://www.codechef.com/START16C/problems/DIRECTN/

Link To Run Code:- CLICK HERE

LIKE! SHARE! COMMENT!

More problems will be added regularly to this series.

Top comments (0)