문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음
문자열 command를 해석할 수 있는 Goal Parser를 가지고 있다. command는 "G", "()", "(al)"로 순서가 정해지지 않은 알파벳으로 구성된다. Goal Parser는 "G"를 문자열 "G", "()"를 문자열 "o", "(al)"를 "al"로 해석한다. 해석된 문자열은 원래 순서대로 연결된다.
문자열 command가 주어졌을 때, command의 Goal Parser의 해석을 반환해라.
#1
Input: command = "G()(al)"
Output: "Goal"
#2
Input: command = "G()()()()(al)"
Output: "Gooooal"
#3
Input: command = "(al)G(al)()()G"
Output: "alGalooG"
class Solution {
public String interpret(String command) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < command.length(); i++){
if(command.charAt(i) == 'G'){
sb.append("G");
}else if(command.charAt(i) == '(' && command.charAt(i + 1) == ')'){
sb.append("o");
i++;
}else{
sb.append("al");
i += 3;
}
}
return sb.toString();
}
}