```
class StringIterator(object):
"""
My initial thought was to use a heap to remember to number of times each letter has, and as we call next(), we subtract 1 from the heap until there is none left and move on to the next letter.
Yet, even though a python dictionary could do this, the problem is, it is an unordered structure. Which is why I try to simulate a heap with two lists instead each containing a letter and its matching number of appearances.
Once this is done all I had to do is to keep count and get rid of the data if the number of appearance becomes 0.
"""
def __init__(self, compressedString):
"""
:type compressedString: str
"""
self.numbers = ''
self.characters= []
for char in compressedString:
if ord(char) < 58 :
self.numbers += char
else:
self.characters.append(char)
self.numbers += ' '
self.numbers = [int(charnum) for charnum in self.numbers.split()]
def next(self):
"""
:rtype: str
"""
if len(self.characters) == 0 or (self.numbers[0] == 0 and len(self.numbers) == 1):
return ' '
if self.numbers[0] == 0:
self.numbers.pop(0)
self.characters.pop(0)
self.numbers[0] -= 1
return self.characters[0]
def hasNext(self):
"""
:rtype: bool
"""
return not (len(self.characters) == 0 or (self.numbers[0] == 0 and len(self.numbers) == 1))
```