All problems
0954MediumMathStringSimulation

Does the Rover Stay Near Home

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1041Robot Bounded In Circle

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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