-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecodeString.py
More file actions
37 lines (29 loc) · 1.14 KB
/
decodeString.py
File metadata and controls
37 lines (29 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#this solution works with some cases but
# not with, for example: 10[leetcode])
class Solution:
def decodeString(self, s):
stack = []
for char in s:
if char != ']':
stack.append(char)
else:
res = ''
while stack[-1] != '[':
res+=stack.pop()
stack.pop()
n = ''
while len(stack) != 0 and stack[-1].isdigit() == True:
n+=stack.pop()
# my attempt to resolve it with my own login
# problem is: it works if it's a single digit
# more than 1 digit(ie: 10) is different
conversion1 = int(str(n))
res * conversion1
stack.append(res*conversion1)
''.join([word[::-1] for word in stack])
#>>> a = ['a', 'b', 'c', 'd']
#>>> ''.join(a)
#'abcd'
return ''.join([word[::-1] for word in stack])
myVar = Solution()
myVar.decodeString("10[leetcode]")