[리트코드] 394. Decode String

박형진·2023년 1월 3일
0

https://leetcode.com/problems/decode-string/description/


1. 코드

class Solution:
	# https://m.blog.naver.com/hyj5419/221960681691 참조
    def decodeString(self, s: str) -> str:
        ans = ''
        stack = []

        for char in s:
            if char != ']':
                stack.append(char)
            else:
                # 알파벳 추출
                temp_str = ''
                while stack and stack[-1] != '[':
                    temp_str += stack.pop()
                temp_str = temp_str[::-1]
                stack.pop()  # '[' 제거

                # 숫자 추출
                num = ''
                while stack and stack[-1].isdigit():
                    num += stack.pop()
                num = int(num[::-1])
                for t in list(num * temp_str):
                    stack.append(t)
        return ''.join(stack)
profile
안녕하세요!

0개의 댓글