Skip to content

Instantly share code, notes, and snippets.

@zeraf29
Created February 1, 2019 07:52
Show Gist options
  • Save zeraf29/49795de079fecd2f8fc48c4f8fd75fc6 to your computer and use it in GitHub Desktop.
Save zeraf29/49795de079fecd2f8fc48c4f8fd75fc6 to your computer and use it in GitHub Desktop.
Directions Reduction solution
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class DirReduction {
public static String[] dirReduc(String[] arr) {
// Your code here.
//for simpler result
ArrayList<String> arrList = new ArrayList<String>();
for(int i=0; i<arr.length; i++) {
if(i==0) { //first elements must insert
arrList.add(arr[i].toUpperCase());
continue;
}
if(
("NORTH".equals(arr[i].toUpperCase()) && arrList.size()>0 && "SOUTH".equals(arrList.get(arrList.size()-1)) )
||
("SOUTH".equals(arr[i].toUpperCase()) && arrList.size()>0 && "NORTH".equals(arrList.get(arrList.size()-1)))
||
("EAST".equals(arr[i].toUpperCase()) && arrList.size()>0 && "WEST".equals(arrList.get(arrList.size()-1)))
||
("WEST".equals(arr[i].toUpperCase()) && arrList.size()>0 && "EAST".equals(arrList.get(arrList.size()-1)))
) {
arrList.remove(arrList.size()-1);
//if before elements is directly oppsite thing, it will be remove with this thing
}else {
arrList.add(arr[i].toUpperCase());
//else this thing will be insert
}
}
return arrList.toArray(new String[arrList.size()]);
}
}
@zeraf29
Copy link
Author

zeraf29 commented Feb 1, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment