본문 바로가기
알고리즘

우아한 테크코스 7번 문제 Cryptogram

by irerin07 2019. 3. 19.
728x90

암호문을 좋아하는 괴짜 개발자 브라운이 이번에는 중복 문자를 이용한 새로운 암호를 만들었습니다.

예를 들어 browoanoommnaon이라는 암호문은 다음과 같은 순서로 해독할 있습니다.

1.  "browoanoommnaon"

2. "browoannaon"

3. "browoaaon"

4. "browoon"

5. "brown"

임의의 문자열 cryptogram 매개변수로 주어질 , 연속하는 중복 문자들을 삭제한 결과를 return 하도록 solution 메서드를 완성해주세요.

 


-----


어찌저찌 풀었는데 이게 효율적인지 아닌지는 모르겠다.


-----


import java.util.*;


public class Main3 {

public static String check(StringBuilder str2, int length) {

StringBuilder str = str2;

System.out.println(str);

for (int i = 0; i < length; i++) {

if (str.length() == 0) {

return "";

}

System.out.println("i : " + i);

if (i+1 < str.length()) {

if (str.charAt(i) == str.charAt(i + 1)) {

System.out.println("str.charAt(i) : " + str.charAt(i));

System.out.println("str.charAt(i+1) : " + str.charAt(i + 1));

str.deleteCharAt(i);

str.deleteCharAt(i);

length -= 2;

check(str, length);

}

}


}

return str.toString();

}


public static void main(String[] args) {

String s = "browoanoommnaon";

// String s = "zyelleyz";

StringBuilder str = new StringBuilder(s);


System.out.println("test : " + (check(str, s.length())));


}

}



728x90