Problem Brief

Encircular

NEW GRADINTERNOA

Build a computer simulation of a mobile robot. The robot moves on an infinite plane, starting from position (0, 0). Its movements are described by a command string consisting of one or more of the following three letters:

  • G instructs the robot to move forward one step.
  • L instructs the robot to turn left in place.
  • R instructs the robot to turn right in place.
  • The robot performs the instructions in a command sequence in an infinite loop. Determine whether there exists some circle such that the robot always moves within the circle.

    Consider the commands R and G executed infinitely. A diagram of the robot's movement looks like:

    The robot will never leave the circle.

    Function Description

    Complete the function doesCircleExist in the editor below. The function must return an array of n strings either YES or NO based on whether the robot is bound within a circle or not, in order of test results.

    doesCircleExist has the following parameter(s):

  • commands[commands[0],...commands[n-1]]: An array of n commands[i] where each represents a list of commands to test.
  • 1Example 1

    Input
    commands = ["G", "L", "RGRG"]
    Output
    ["NO", "YES", "YES"]
    Explanation
    There are n = 2 commands: 1. For commands[0] = "G", the robot will move forward forever (G -> G-> G -> ...) without ever turning or being restricted to a circle. Set index 0 of the return array to NO. 2. For commands[1] = "L", the robot will just turn 90 degree left forever without ever moving forward (because there is no "G" instruction). The robot is effectively trapped at one spot, thus bound within a circle. Set index 1 of the return array to YES. 3. For commands[2] = "GRGR" , concatenate the string to "GRGRGRGR" and it will follow the circular path in the statement example. Set index 2 of the return array to YES.

    Constraints

    Limits and guarantees your solution can rely on.

  • 1 <= |commands[i]| <= 2500
  • 1 <= n <= 10
  • Each command consists of G, L, and R only.
  • public String[] doesCircleExist(String[] commands) {
      // write your code here
    }
    
    Input

    commands

    ["G", "L", "RGRG"]

    Output

    ["NO", "YES", "YES"]

    Sign in to submit your solution.