-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bounded Robot
52 lines (50 loc) · 1.39 KB
/
Bounded Robot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Solution {
public boolean isRobotBounded(String instructions) {
// robot is bounded if:
// end position is 0,0
// OR
// end direction is not North
char direction = 'N';
int x = 0;
int y = 0;
for(int i=0; i<instructions.length(); i++){
if(instructions.charAt(i)=='G'){
if(direction=='N'){
y++;
}
if(direction=='W'){
x++;
}
if(direction=='E'){
x--;;
}
if(direction=='S'){
y--;
}
}
else if(instructions.charAt(i)=='L'){
if (direction == 'N') {
direction = 'W';
} else if (direction == 'S') {
direction = 'E';
} else if (direction == 'W' ){
direction = 'S';
} else {
direction = 'N';
}
}
else{
if (direction == 'N') {
direction = 'E';
} else if (direction == 'S') {
direction = 'W';
} else if (direction == 'W') {
direction = 'N';
} else {
direction = 'S';
}
}
}
return (x == 0 && y == 0) || (direction != 'N');
}
}