Input: instructions ="GGLLGG"Output: trueExplanation: 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 returntrue.
Example 2:
1
2
3
4
5
6
7
Input: instructions ="GG"Output: falseExplanation: 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 returnfalse.
Example 3:
1
2
3
4
5
6
7
8
9
10
11
12
13
Input: instructions ="GL"Output: trueExplanation: 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 returntrue.
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.
classSolution {
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;
} elseif (c =='L') {
dir = (dir +3) %4;
} else {
dir = (dir +1) %4;
}
}
return (x ==0&& y ==0) || dir !=0;
}
};
classSolution {
publicbooleanisRobotBounded(String instructions) {
int[][] dirs =newint[][]{{0,1},{1,0},{0,-1},{-1,0}}; // N, E, S, Wint x = 0, y = 0, dir = 0;
for (char c : instructions.toCharArray()) {
if (c =='G') {
x += dirs[dir][0];
y += dirs[dir][1];
} elseif (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
classSolution {
funisRobotBounded(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 = 0for (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
classSolution:
defisRobotBounded(self, instructions: str) -> bool:
dirs = [(0,1),(1,0),(0,-1),(-1,0)] # N, E, S, W x = y = dir =0for c in instructions:
if c =='G':
x += dirs[dir][0]
y += dirs[dir][1]
elif c =='L':
dir = (dir +3) %4else:
dir = (dir +1) %4return (x ==0and y ==0) or dir !=0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
impl Solution {
pubfnis_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
classSolution {
isRobotBounded(instructions: string):boolean {
constdirs= [[0,1],[1,0],[0,-1],[-1,0]]; // N, E, S, W
letx=0, y=0, dir=0;
for (constcofinstructions) {
if (c==='G') {
x+=dirs[dir][0];
y+=dirs[dir][1];
} elseif (c==='L') {
dir= (dir+3) %4;
} else {
dir= (dir+1) %4;
}
}
return (x===0&&y===0) ||dir!==0;
}
}