Show problems as
Classic problem #1041MediumMathStringSimulation

Robot Bounded In Circle

Real-world version: Does the Rover Stay Near Home
Tracked in this browser only
Read Robot Bounded In Circle on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A rover starts at the origin facing north and follows instructions, one character at a time: 'G' moves it one step forward in the direction it faces, 'L' turns it a quarter turn to the left, and 'R' a quarter turn to the right.

The rover repeats the whole list of instructions over and over, without end. Return true when there is some circle around the origin that the rover never leaves.

Examples

Example 1

Input
instructions = "GGLRGGRL"
Output
false

A left turn and a right turn cancel each other out, so the rover ends four steps north still facing north, and every repeat carries it four further.

Example 2

Input
instructions = "GLGLGLGL"
Output
true

The rover traces a unit square and comes back to where it started facing north, so it goes round that square forever.

Example 3

Input
instructions = "GGRGGR"
Output
true

The rover ends two steps north and two east facing south. Repeating turns the whole journey a half turn each time, so the two halves cancel and it stays near home.

Constraints

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

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def is_robot_bounded(instructions: str) -> bool:
Java
public boolean isRobotBounded(String instructions)