Skip to content

Commit 6fbca95

Browse files
Create DecodedStringAtIndex.py
1 parent 247a854 commit 6fbca95

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Definition for a binary tree node.
2+
import heapq
3+
import unittest
4+
5+
# Read about enumerate in python
6+
from collections import defaultdict
7+
from typing import List
8+
9+
class DecodedStringAtIndex(unittest.TestCase):
10+
11+
def decodeAtIndex(self, S: str, K: int) -> str:
12+
13+
count = 0
14+
i = 0
15+
curr = ""
16+
while i < len(S):
17+
if count == K:
18+
return S[i-1]
19+
elif S[i].isalpha():
20+
curr += S[i]
21+
i += 1
22+
count += 1
23+
elif S[i].isdigit():
24+
j = i
25+
while j < len(S) and S[j].isdigit():
26+
j += 1
27+
number = int(S[i:j])
28+
multiplyLength = i * number
29+
if multiplyLength > K:
30+
return self.decodeAtIndex(S[:i], K % i)
31+
elif multiplyLength == K:
32+
return curr[-1]
33+
else:
34+
curr = curr * number
35+
i = j
36+
count = multiplyLength
37+
38+
return ""
39+
40+
def test_Leetcode(self):
41+
self.assertEqual("o", self.decodeAtIndex("leet2code3", 10))
42+
self.assertEqual("h", self.decodeAtIndex("ha22", 5))
43+
self.assertEqual("a", self.decodeAtIndex("a2345678999999999999999", 1))
44+
45+
def test_Edgecase(self):
46+
self.assertEqual("t", self.decodeAtIndex("leet2code3", 8))
47+
return
48+
49+
if __name__ == '__main__':
50+
unittest.main()

0 commit comments

Comments
 (0)