Created
October 7, 2024 21:39
-
-
Save hongtaoh/67d159790440bbd21f8bdf7889687c41 to your computer and use it in GitHub Desktop.
Solution to Leetcode 394 Decode String
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def decodeString(self, s: str) -> str: | |
stack = [] | |
for i in s: | |
if i != "]": | |
stack.append(i) | |
else: | |
substring = "" | |
while stack[-1] != "[": | |
substring = stack.pop() + substring | |
# pop "[" | |
stack.pop() | |
k = "" | |
while stack and stack[-1].isdigit(): | |
k = stack.pop() + k | |
stack.append(int(k) * substring) | |
return "".join(stack) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment