Given a grid of size N X M and a robot is placed at cell (N – 1, M – 1). Also, given string str which consists only of the characters ‘U’ (Up), ‘D’ (Down), ‘L’ (Left) and ‘R’ (Right) representing the moves the robot is going to perform within the grid. The task is to find whether the robot will be safe at the end of the last move. Robot is said to be safe if it is within the bounds of the grid.
Note: Consider that the rectangular grid is present below the number line with the top-left corner lying on the origin. 
Examples: 
 
Input: N = 1, M = 1, str = “R”
Output: No
As there is only 1 cell, no movement is allowed.
Input: N = 2, M = 3, str = “LLRU”
Output: Yes
Approach: For every move, update the position of the robot inside the grid. if at any move the position of the robot is outside the grid then the output will be No else print Yes if for all the moves, the robot is within the bounds of the grid.
Below is the implementation of the above approach: 
 
C++
| // C++ implementation of the approach#include <bits/stdc++.h>usingnamespacestd;// Function that returns true if the robot is safeboolisSafe(intN, intM, string str){    intcoll = 0, colr = 0, rowu = 0, rowd = 0;    for(inti = 0; i < str.length(); i++) {        // If current move is "L" then        // increase the counter of coll        if(str[i] == 'L') {            coll++;            if(colr > 0) {                colr--;            }            // If value of coll is equal to            // column then break            if(coll == M) {                break;            }        }        // If current move is "R" then        // increase the counter of colr        elseif(str[i] == 'R') {            colr++;            if(coll > 0) {                coll--;            }            // If value of colr is equal to            // column then break            if(colr == M) {                break;            }        }        // If current move is "U" then        // increase the counter of rowu        elseif(str[i] == 'U') {            -rowu++;            if(rowd > 0) {                rowd--;            }            // If value of rowu is equal to            // row then break            if(rowu == N) {                break;            }        }        // If current move is "D" then        // increase the counter of rowd        elseif(str[i] == 'D') {            rowd++;            if(rowu > 0) {                rowu--;            }            // If value of rowd is equal to            // row then break            if(rowd == N) {                break;            }        }    }    // If robot is within the bounds of the grid    if(abs(rowd) < N && abs(rowu) < N        && abs(coll) < M && abs(colr) < M) {        returntrue;    }    // Unsafe    returnfalse;}// Driver codeintmain(){    intN = 1, M = 1;    string str = "R";    if(isSafe(N, M, str))        cout << "Yes";    else        cout << "No";    return0;} | 
Java
| // Java implementation of the approachclassGFG {    // Function that returns true if the robot is safe    staticbooleanisSafe(intN, intM, char[] str)    {        intcoll = 0, colr = 0, rowu = 0, rowd = 0;        for(inti = 0; i < str.length; i++) {            // If current move is "L" then            // increase the counter of coll            if(str[i] == 'L') {                coll++;                if(colr > 0) {                    colr--;                }                // If value of coll is equal to                // column then break                if(coll == M) {                    break;                }            }            // If current move is "R" then            // increase the counter of colr            elseif(str[i] == 'R') {                colr++;                if(coll > 0) {                    coll--;                }                // If value of colr is equal to                // column then break                if(colr == M) {                    break;                }            }            // If current move is "U" then            // increase the counter of rowu            elseif(str[i] == 'U') {                rowu++;                if(rowd > 0) {                    rowd--;                }                // If value of rowu is equal to                // row then break                if(rowu == N) {                    break;                }            }            // If current move is "D" then            // increase the counter of rowd            elseif(str[i] == 'D') {                rowd++;                if(rowu > 0) {                    rowu--;                }                // If value of rowd is equal to                // row then break                if(rowd == N) {                    break;                }            }        }        // If robot is within the bounds of the grid        if(Math.abs(rowd) < N && Math.abs(rowu) < N            && Math.abs(coll) < M && Math.abs(colr) < M) {            returntrue;        }        // Unsafe        returnfalse;    }    // Driver code    publicstaticvoidmain(String[] args)    {        intN = 1, M = 1;        String str = "R";        if(isSafe(N, M, str.toCharArray()))            System.out.println("Yes");        else            System.out.println("No");    }}// This code is contributed by 29AjayKumar | 
Python3
| # Python 3 implementation of the approach# Function that returns true # if the robot is safedefisSafe(N, M, str):    coll =0    colr =0    rowu =0    rowd =0    fori inrange(len(str)):                # If current move is "L" then        # increase the counter of coll        if(str[i] =='L'):            coll +=1            if(colr > 0):                colr -=1            # If value of coll is equal to            # column then break            if(coll ==M):                break        # If current move is "R" then        # increase the counter of colr        elif(str[i] =='R'):            colr +=1            if(coll > 0):                coll -=1            # If value of colr is equal to            # column then break            if(colr ==M):                break        # If current move is "U" then        # increase the counter of rowu        elif(str[i] =='U'):            rowu +=1            if(rowd > 0):                rowd -=1            # If value of rowu is equal to            # row then break            if(rowu ==N):                break        # If current move is "D" then        # increase the counter of rowd        elif(str[i] =='D'):            rowd +=1            if(rowu > 0):                rowu -=1            # If value of rowd is equal to            # row then break            if(rowd ==N):                break    # If robot is within the bounds of the grid    if(abs(rowd) < N andabs(rowu) < N and        abs(coll) < M andabs(colr) < M):        returnTrue    # Unsafe    returnFalse# Driver codeif__name__ =='__main__':    N =1    M =1    str="R"    if(isSafe(N, M, str)):        print("Yes")    else:        print("No")# This code is contributed by# Surendra_Gangwar | 
C#
| // C# implementation of the approachusingSystem;classGFG {    // Function that returns true if the robot is safe    staticboolisSafe(intN, intM, char[] str)    {        intcoll = 0, colr = 0, rowu = 0, rowd = 0;        for(inti = 0; i < str.Length; i++) {            // If current move is "L" then            // increase the counter of coll            if(str[i] == 'L') {                coll++;                if(colr > 0) {                    colr--;                }                // If value of coll is equal to                // column then break                if(coll == M) {                    break;                }            }            // If current move is "R" then            // increase the counter of colr            elseif(str[i] == 'R') {                colr++;                if(coll > 0) {                    coll--;                }                // If value of colr is equal to                // column then break                if(colr == M) {                    break;                }            }            // If current move is "U" then            // increase the counter of rowu            elseif(str[i] == 'U') {                rowu++;                if(rowd > 0) {                    rowd--;                }                // If value of rowu is equal to                // row then break                if(rowu == N) {                    break;                }            }            // If current move is "D" then            // increase the counter of rowd            elseif(str[i] == 'D') {                rowd++;                if(rowu > 0) {                    rowu--;                }                // If value of rowd is equal to                // row then break                if(rowd == N) {                    break;                }            }        }        // If robot is within the bounds of the grid        if(Math.Abs(rowd) < N && Math.Abs(rowu) < N            && Math.Abs(coll) < M && Math.Abs(colr) < M) {            returntrue;        }        // Unsafe        returnfalse;    }    // Driver code    publicstaticvoidMain(String[] args)    {        intN = 1, M = 1;        String str = "R";        if(isSafe(N, M, str.ToCharArray()))            Console.WriteLine("Yes");        else            Console.WriteLine("No");    }}// This code has been contributed by 29AjayKumar | 
Javascript
| <script>// Javascript implementation of the approach// Function that returns true if the robot is safefunctionisSafe(N, M, str){    varcoll = 0, colr = 0, rowu = 0, rowd = 0;    for(vari = 0; i < str.length; i++) {        // If current move is "L" then        // increase the counter of coll        if(str[i] == 'L') {            coll++;            if(colr > 0) {                colr--;            }            // If value of coll is equal to            // column then break            if(coll == M) {                break;            }        }        // If current move is "R" then        // increase the counter of colr        elseif(str[i] == 'R') {            colr++;            if(coll > 0) {                coll--;            }            // If value of colr is equal to            // column then break            if(colr == M) {                break;            }        }        // If current move is "U" then        // increase the counter of rowu        elseif(str[i] == 'U') {            -rowu++;            if(rowd > 0) {                rowd--;            }            // If value of rowu is equal to            // row then break            if(rowu == N) {                break;            }        }        // If current move is "D" then        // increase the counter of rowd        elseif(str[i] == 'D') {            rowd++;            if(rowu > 0) {                rowu--;            }            // If value of rowd is equal to            // row then break            if(rowd == N) {                break;            }        }    }    // If robot is within the bounds of the grid    if(Math.abs(rowd) < N && Math.abs(rowu) < N        && Math.abs(coll) < M && Math.abs(colr) < M) {        returntrue;    }    // Unsafe    returnfalse;}// Driver codevarN = 1, M = 1;varstr = "R";if(isSafe(N, M, str))    document.write( "Yes");else    document.write( "No");</script> | 
No
Time Complexity: O(|str|)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!


 
                                    








