
g0301_0400.s0394_decode_string.Solution.py Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
# #Medium #Top_100_Liked_Questions #String #Stack #Recursion #Level_1_Day_14_Stack #Udemy_Strings
# #Big_O_Time_O(n)_Space_O(n) #2024_06_08_Time_28_ms_(91.14%)_Space_16.5_MB_(41.29%)
class Solution:
def __init__(self):
self.i = 0
def decodeString(self, s: str) -> str:
count = 0
sb = []
while self.i < len(s):
c = s[self.i]
self.i += 1
if c.isalpha():
sb.append(c)
elif c.isdigit():
count = count * 10 + int(c)
elif c == ']':
break
elif c == '[':
# sub problem
repeat = self.decodeString(s)
sb.append(repeat * count)
count = 0
return ''.join(sb)
© 2015 - 2025 Weber Informatics LLC | Privacy Policy