Problem

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.

Examples

Example 1:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Input: instructions = "GGLLGG"
Output: true
Explanation: The robot is initially at (0, 0) facing the north direction.
"G": move one step. Position: (0, 1). Direction: North.
"G": move one step. Position: (0, 2). Direction: North.
"L": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: West.
"L": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: South.
"G": move one step. Position: (0, 1). Direction: South.
"G": move one step. Position: (0, 0). Direction: South.
Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (0, 2) --> (0, 1) --> (0, 0).
Based on that, we return true.

Example 2:

1
2
3
4
5
6
7
Input: instructions = "GG"
Output: false
Explanation: The robot is initially at (0, 0) facing the north direction.
"G": move one step. Position: (0, 1). Direction: North.
"G": move one step. Position: (0, 2). Direction: North.
Repeating the instructions, keeps advancing in the north direction and does not go into cycles.
Based on that, we return false.

Example 3:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
Input: instructions = "GL"
Output: true
Explanation: The robot is initially at (0, 0) facing the north direction.
"G": move one step. Position: (0, 1). Direction: North.
"L": turn 90 degrees anti-clockwise. Position: (0, 1). Direction: West.
"G": move one step. Position: (-1, 1). Direction: West.
"L": turn 90 degrees anti-clockwise. Position: (-1, 1). Direction: South.
"G": move one step. Position: (-1, 0). Direction: South.
"L": turn 90 degrees anti-clockwise. Position: (-1, 0). Direction: East.
"G": move one step. Position: (0, 0). Direction: East.
"L": turn 90 degrees anti-clockwise. Position: (0, 0). Direction: North.
Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (-1, 1) --> (-1, 0) --> (0, 0).
Based on that, we return true.

Constraints:

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

Source

Method 1 – Simulate Directions and Check for Cycle

Intuition

The robot moves in a sequence of instructions, turning left or right or moving forward. If, after one cycle of instructions, the robot is back at the origin or is facing a direction other than north, it will stay within a circle when repeating the instructions. Otherwise, it will wander off to infinity.

Approach

  1. Represent directions as vectors: North [0,1], East [1,0], South [0,-1], West [-1,0].
  2. Track the robot’s position (x, y) and direction index dirIdx (0: North, 1: East, 2: South, 3: West).
  3. For each instruction:
    • ‘G’: Move forward in the current direction.
    • ‘L’: Turn left (dirIdx = (dirIdx + 3) % 4).
    • ‘R’: Turn right (dirIdx = (dirIdx + 1) % 4).
  4. After one cycle, if the robot is back at the origin or not facing north, it is bounded in a circle.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
public:
	bool isRobotBounded(string instructions) {
		vector<pair<int,int>> dirs = {{0,1},{1,0},{0,-1},{-1,0}}; // N, E, S, W
		int x = 0, y = 0, dir = 0;
		for (char c : instructions) {
			if (c == 'G') {
				x += dirs[dir].first;
				y += dirs[dir].second;
			} else if (c == 'L') {
				dir = (dir + 3) % 4;
			} else {
				dir = (dir + 1) % 4;
			}
		}
		return (x == 0 && y == 0) || dir != 0;
	}
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
func isRobotBounded(instructions string) bool {
	dirs := [][2]int{{0,1},{1,0},{0,-1},{-1,0}}
	x, y, dir := 0, 0, 0
	for _, c := range instructions {
		if c == 'G' {
			x += dirs[dir][0]
			y += dirs[dir][1]
		} else if c == 'L' {
			dir = (dir + 3) % 4
		} else {
			dir = (dir + 1) % 4
		}
	}
	return (x == 0 && y == 0) || dir != 0
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
	public boolean isRobotBounded(String instructions) {
		int[][] dirs = new int[][]{{0,1},{1,0},{0,-1},{-1,0}}; // N, E, S, W
		int x = 0, y = 0, dir = 0;
		for (char c : instructions.toCharArray()) {
			if (c == 'G') {
				x += dirs[dir][0];
				y += dirs[dir][1];
			} else if (c == 'L') {
				dir = (dir + 3) % 4;
			} else {
				dir = (dir + 1) % 4;
			}
		}
		return (x == 0 && y == 0) || dir != 0;
	}
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
	fun isRobotBounded(instructions: String): Boolean {
		val dirs = arrayOf(intArrayOf(0,1), intArrayOf(1,0), intArrayOf(0,-1), intArrayOf(-1,0))
		var x = 0; var y = 0; var dir = 0
		for (c in instructions) {
			when (c) {
				'G' -> { x += dirs[dir][0]; y += dirs[dir][1] }
				'L' -> dir = (dir + 3) % 4
				'R' -> dir = (dir + 1) % 4
			}
		}
		return (x == 0 && y == 0) || dir != 0
	}
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution:
	def isRobotBounded(self, instructions: str) -> bool:
		dirs = [(0,1),(1,0),(0,-1),(-1,0)] # N, E, S, W
		x = y = dir = 0
		for c in instructions:
			if c == 'G':
				x += dirs[dir][0]
				y += dirs[dir][1]
			elif c == 'L':
				dir = (dir + 3) % 4
			else:
				dir = (dir + 1) % 4
		return (x == 0 and y == 0) or dir != 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
impl Solution {
	pub fn is_robot_bounded(instructions: String) -> bool {
		let dirs = [(0,1),(1,0),(0,-1),(-1,0)];
		let (mut x, mut y, mut dir) = (0, 0, 0);
		for c in instructions.chars() {
			match c {
				'G' => { x += dirs[dir].0; y += dirs[dir].1; },
				'L' => { dir = (dir + 3) % 4; },
				_ => { dir = (dir + 1) % 4; },
			}
		}
		(x == 0 && y == 0) || dir != 0
	}
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
	isRobotBounded(instructions: string): boolean {
		const dirs = [[0,1],[1,0],[0,-1],[-1,0]]; // N, E, S, W
		let x = 0, y = 0, dir = 0;
		for (const c of instructions) {
			if (c === 'G') {
				x += dirs[dir][0];
				y += dirs[dir][1];
			} else if (c === 'L') {
				dir = (dir + 3) % 4;
			} else {
				dir = (dir + 1) % 4;
			}
		}
		return (x === 0 && y === 0) || dir !== 0;
	}
}

Complexity

  • Time complexity: O(n), where n is the length of the instructions string.
  • 🧺 Space complexity: O(1).