-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectionsReduction.java
32 lines (27 loc) · 1.09 KB
/
DirectionsReduction.java
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
import java.util.ArrayList;
import java.util.Arrays;
public class DirectionsReduction {
public static String[] dirReduc(String[] arr) {
ArrayList<String> arList = new ArrayList<>();
arList.addAll(Arrays.asList(arr));
for (int i = 0; i < arList.size() - 1; i++) {
switch (arList.get(i)) {
case "NORTH":
if (arList.get(i+1) == "SOUTH") { arList.remove(i); arList.remove(i); i = -1; }
break;
case "SOUTH":
if (arList.get(i+1) == "NORTH") { arList.remove(i); arList.remove(i); i = -1; }
break;
case "EAST":
if (arList.get(i+1) == "WEST") { arList.remove(i); arList.remove(i); i = -1; }
break;
case "WEST":
if (arList.get(i+1) == "EAST") { arList.remove(i); arList.remove(i); i = -1; }
break;
}
}
String[] out = new String[arList.size()];
arList.toArray(out);
return out;
}
}