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()