CodeWars 다섯 번째 문제

Updated:

Directions Reduction

public class DirReduction {
    public static String[] dirReduc(String[] arr) {
        
      List<String> dirList = new ArrayList<String>(Arrays.asList(arr));
      int i = 0;
      
      do {
        String temp = dirList.get(i) + dirList.get(i+1);
        
        if("WESTEAST".equals(temp) || "EASTWEST".equals(temp) ||
            "NORTHSOUTH".equals(temp) || "SOUTHNORTH".equals(temp)) {
          
          dirList.remove(i+1);
          dirList.remove(i);
          
          if(i > 0) {
            i--;
          }
        }
        else {
          i++;
        }
        
      } while( i < dirList.size() -1);   
      

      return dirList.toArray(new String[dirList.size()]);
    }
}

*첫번째 난관 : WEST와 EAST, SOUTH와 NORTH가 붙어있다면 어떻게 줄여 나갈 것인가?

*두번째 난관 : Array와 List의 변환. 자바 12를 쓰고 있는데 달라진건가? 내가 감을 잃은건가…암튼.

/* 배열에서 리스트로 변환 : 1번 방식 */
String[] strArray = {"a", "b", "c"};
ArrayList<String> strList = new ArrayList<String>(Arrays.asList(strArray));

/* 배열에서 리스트로 변환 : 2번 방식 */
String[] strArray = {"a", "b", "c"};
List<String> strList = new ArrayList<String>();
Collections.addAll(strList, strArray);

List<String> strList = new ArrayList<String>();
strList.add("a");
strList.add("b");
strList.add("c");
 
/* 리스트에서 배열로 변환 : 2번 방식 */
String[] strArray = strList.toArray(new String[strList.size()]);
System.out.println(Arrays.toString(strArray));