Robot Bounded In Circle

Medium Topics Company Tags

A robot starts at (0, 0) facing north on an infinite plane. It executes the characters of instructions in order, repeating the entire string forever.

  • G moves forward one unit in its current direction.
  • L turns left by 90 degrees without moving.
  • R turns right by 90 degrees without moving.

Return true if the robot stays within some fixed circle forever, and false if it travels arbitrarily far from the origin.

Example 1:

Input: instructions = "GR"

Output: true

Explanation: After four repetitions, the robot traces a unit square and returns to its starting position and direction. Repeating that square keeps it bounded.

Example 2:

Input: instructions = "GLGR"

Output: false

Explanation: Each repetition moves one unit north and one unit west, then restores the original northward direction. The robot keeps drifting away from the origin.

Example 3:

Input: instructions = "LR"

Output: true

Explanation: These instructions only turn the robot left and then right. It never moves from the origin.

Constraints:

  • 1 <= instructions.length <= 100
  • Only G, L, and R occur.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.