Decode String

Problem Id: 394 Difficulty: Medium


Intuition

Solution


class Solution:
    def decodeString(self, s: str) -> str:
        ans = ''
        if not s:
            return s

        i = 0
        stack = []
        tmp = ''
        count = ''
        for c in s:
            if c in string.ascii_letters:
                tmp += c
            elif c in string.digits:
                count += c
            elif c == '[':
                stack.append((tmp, count))
                tmp = ''
                count = ''
            else:
                tmp_poped, count = stack.pop()
                tmp = tmp_poped + int(count) * tmp
                count = ''
        return tmp