-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.py
32 lines (28 loc) · 899 Bytes
/
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
class StringIterator:
def __init__(self, compressedString: str):
self.d = []
self.p = 0
n = len(compressedString)
i = 0
while i < n:
c = compressedString[i]
x = 0
i += 1
while i < n and compressedString[i].isdigit():
x = x * 10 + int(compressedString[i])
i += 1
self.d.append([c, x])
def next(self) -> str:
if not self.hasNext():
return ' '
ans = self.d[self.p][0]
self.d[self.p][1] -= 1
if self.d[self.p][1] == 0:
self.p += 1
return ans
def hasNext(self) -> bool:
return self.p < len(self.d) and self.d[self.p][1] > 0
# Your StringIterator object will be instantiated and called as such:
# obj = StringIterator(compressedString)
# param_1 = obj.next()
# param_2 = obj.hasNext()