Imagine a robot placed on an infinite 2D plane at the origin (0, 0), initially facing north. The robot receives a sequence of instructions and will repeat them infinitely.
The robot understands three commands:
'G'- Move forward 1 unit in the current direction'L'- Turn 90 degrees left (counterclockwise)'R'- Turn 90 degrees right (clockwise)
The Challenge: Determine if the robot will stay within a bounded circular area forever, or if it will eventually escape to infinity.
Key Insight: If after executing the instruction sequence once, the robot either returns to the origin OR is not facing north, then it will eventually form a cycle and stay bounded!
Example: Instructions "GGLLGG" will make the robot walk in a square pattern, never escaping.
Input & Output
Visualization
Time & Space Complexity
Single pass through the instruction string of length n
Only storing position coordinates and direction, constant extra space
Constraints
- 1 โค instructions.length โค 100
-
instructions[i] is
'G','L'or'R' - The robot will repeat the instructions infinitely