[LeetCode] Goal Parser Interpretation

아르당·2026년 6월 12일

LeetCode

목록 보기
355/602
post-thumbnail

문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음

Problem

문자열 command를 해석할 수 있는 Goal Parser를 가지고 있다. command는 "G", "()", "(al)"로 순서가 정해지지 않은 알파벳으로 구성된다. Goal Parser는 "G"를 문자열 "G", "()"를 문자열 "o", "(al)"를 "al"로 해석한다. 해석된 문자열은 원래 순서대로 연결된다.

문자열 command가 주어졌을 때, command의 Goal Parser의 해석을 반환해라.

Example

#1
Input: command = "G()(al)"
Output: "Goal"

#2
Input: command = "G()()()()(al)"
Output: "Gooooal"

#3
Input: command = "(al)G(al)()()G"
Output: "alGalooG"

Constraints

  • 1 <= command.length <= 100
  • command는 "G", "()", "(al)"로 구성된다.

Solved

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();
    }
}
profile
내 마음대로 코드 작성하는 세상

0개의 댓글