From ab1a6bae54e924dd448ac16cb44c638e14a00528 Mon Sep 17 00:00:00 2001 From: ccalderon Date: Fri, 25 Mar 2022 15:31:59 -0500 Subject: [PATCH] feat: Leetcode 394: Decode String. --- 394-decode-string.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 394-decode-string.py diff --git a/394-decode-string.py b/394-decode-string.py new file mode 100644 index 000000000..445a81cfd --- /dev/null +++ b/394-decode-string.py @@ -0,0 +1,20 @@ +class Solution: + def decodeString(self, s: str) -> str: + stack = [] + + for char in s: + if char is not "]": + stack.append(char) + else: + sub_str = "" + while stack[-1] is not "[": + sub_str = stack.pop()+sub_str + stack.pop() + + multiplier = "" + while stack and stack[-1].isdigit(): + multiplier = stack.pop() + multiplier + + stack.append(int(multiplier)*sub_str) + + return "".join(stack) \ No newline at end of file