Skip to content

Latest commit

 

History

History
61 lines (44 loc) · 1.58 KB

_394. Decode String.md

File metadata and controls

61 lines (44 loc) · 1.58 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 15, 2024

Last updated : June 15, 2024


Related Topics : String, Stack, Recursion

Acceptance Rate : 60.36 %


Solutions

Python

class Solution:
    def decodeString(self, s: str) -> str:
        output = []
        currentIndx = 0

        while currentIndx < len(s) :
            if s[currentIndx].isnumeric() :
                numStart = currentIndx
                while s[currentIndx].isnumeric() :
                    currentIndx += 1

                bracketStart = currentIndx
                num = int(s[numStart:bracketStart])

                numOpeners = 1
                while numOpeners > 0 :
                    currentIndx += 1
                    
                    if s[currentIndx] == '[' :
                        numOpeners += 1
                    elif s[currentIndx] == ']' :
                        numOpeners -= 1

                ending = currentIndx
                middle = self.decodeString(s[bracketStart + 1: ending])

                output.extend(num * [middle])
                currentIndx += 1
            else :
                output.append(s[currentIndx])
                currentIndx += 1
        
        return ''.join(output)