Robot Bounded In Circle

On an infinite plane, a robot initially stands at (0, 0) and faces north. Note that:

  • The north direction is the positive direction of the y-axis.
  • The south direction is the negative direction of the y-axis.
  • The east direction is the positive direction of the x-axis.
  • The west direction is the negative direction of the x-axis.

The robot can receive one of three instructions:

  • "G": go straight 1 unit.
  • "L": turn 90 degrees to the left, i.e. anti-clockwise direction.
  • "R": turn 90 degrees to the right, i.e. clockwise direction.

The robot performs the instructions given in order, and repeats them forever.

Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.

Example 1
Inputinstructions = "GGLLGG"
Outputtrue
Repeating the instructions makes the robot go into the cycle (0, 0) --> (0, 1) --> (0, 2) --> (0, 1) --> (0, 0), so the robot stays bounded.
Example 2
Inputinstructions = "GG"
Outputfalse
Repeating the instructions keeps advancing in the north direction and does not go into cycles, so the robot is not bounded.

Constraints

  • 1 <= instructions.length <= 100
  • instructions[i] is 'G', 'L' or, 'R'.

Asked at 11 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate