😎풀이

  1. s 순회
    1-1. 숫자와 문자를 별도로 저장하며, 새로운 depth가 생길 경우 현재 값들을 Stack 형태로 저장
    1-2. depth가 종료될 경우, 현재까지 저장된 문자열을 반복하여 기존 문자열과 더함
    1-3. 최종 디코딩된 문자열 반환
function decodeString(s: string): string {
    const str: string[] = []
    const repeat = []
    let curStr = ''
    let curRepeat = ''
    for(const char of s) {
        if(char.match(/\d/)) {
            curRepeat += char
        } else if(char === '[') {
            repeat.push(Number(curRepeat))
            str.push(curStr)
            curRepeat = ''
            curStr = ''
        } else if(char.match(/[a-z]/)) {
            curStr += char
        } else if(char === ']') {
            const popedRepeat = repeat.pop()
            const popedStr = str.pop()
            curStr = popedStr + curStr.repeat(popedRepeat)
        }
    }
    return curStr
};

profile
내 지식을 공유할 수 있는 대담함

0개의 댓글