-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_1041.java
More file actions
40 lines (38 loc) · 1.4 KB
/
_1041.java
File metadata and controls
40 lines (38 loc) · 1.4 KB
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
package com.github.aditya;
public class _1041 {
public static class Solution {
public boolean isRobotBounded(String instructions) {
String dir = "+y";
int x = 0, y = 0;
for (int i = 0; i < instructions.length(); i++) {
if (instructions.charAt(i) == 'G') {
switch (dir) {
case "+y": y++; break;
case "-y": y--; break;
case "+x": x++; break;
case "-x": x--; break;
}
} else if (instructions.charAt(i) == 'R') {
switch (dir) {
case "+y": dir = "+x"; break;
case "-y": dir = "-x"; break;
case "+x": dir = "-y"; break;
case "-x": dir = "+y"; break;
}
} else if (instructions.charAt(i) == 'L') {
switch (dir) {
case "+y": dir = "-x"; break;
case "-y": dir = "+x"; break;
case "+x": dir = "+y"; break;
case "-x": dir = "-y"; break;
}
}
}
if (x == 0 && y == 0)
return true;
if (dir.equals("+y"))
return false;
return true;
}
}
}