Skip to content

Instantly share code, notes, and snippets.

@hongtaoh
Created October 7, 2024 21:39
Show Gist options
  • Save hongtaoh/67d159790440bbd21f8bdf7889687c41 to your computer and use it in GitHub Desktop.
Save hongtaoh/67d159790440bbd21f8bdf7889687c41 to your computer and use it in GitHub Desktop.
Solution to Leetcode 394 Decode String
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