Created
February 1, 2019 07:52
-
-
Save zeraf29/49795de079fecd2f8fc48c4f8fd75fc6 to your computer and use it in GitHub Desktop.
Directions Reduction solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for codewars solution about https://www.codewars.com/kata/550f22f4d758534c1100025a/train/java