-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
45 lines (41 loc) · 1.37 KB
/
Solution.py
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
38
39
40
41
42
43
44
45
class Solution:
def decodeAtIndex(self, s: str, k: int) -> str:
# The method below will exceed the memory limit
# ----------------------------------------------------
# base_str = ""
# repeat_times = 1
# for ch in s:
# if ch.isdigit():
# repeat_times *= int(ch)
# else:
# base_str = base_str * repeat_times + ch
# repeat_times = 1
# if len(base_str) * repeat_times >= k:
# return base_str[(k % len(base_str)) - 1]
# ----------------------------------------------------
length = 0
pos = 0
while pos < len(s) and length < k:
if s[pos].isdigit():
length *= int(s[pos])
else:
length += 1
pos += 1
pos -= 1
while pos >= 0:
if s[pos].isdigit():
length //= int(s[pos])
k %= length
else:
if k == length or k == 0:
return s[pos]
length -= 1
pos -= 1
# for ch in range(pos - 1, -1, -1):
# if s[ch].isdigit():
# length //= int(s[ch])
# k %= length
# else:
# if k == length or k == 0:
# return s[ch]
# length -= 1