Decoded String at Index LeetCode solution in Python

1
class Solution:
2
def decodeAtIndex(self, S: str, K: int) -> str:
3
idx = {}
4
acclens = [0]
5
prevd = 1
6
j = 0
7
for i, c in enumerate(S + "1"):
8
if c.isalpha():
9
idx[acclens[-1] * prevd + j] = i
10
j += 1
11
else:
12
acclens.append(acclens[-1] * prevd + j)
13
prevd = int(c)
14
j = 0
15
k = K - 1
16
for al in reversed(acclens[1:]):
17
k %= al
18
if k in idx:
19
return S[idx[k]]
20
return None # should never reach this

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0