Straightforward Python without list or queue


  • 0
    class StringIterator(object):
    
        def __init__(self, compressedString):
            self.cs = compressedString
            self.pos = 0
            self.nextPos = len(self.cs)
            self.count = self.countNext()
            
        def next(self):
            if not self.count:
                if self.nextPos == len(self.cs): return ' '
                self.pos = self.nextPos
                self.count = self.countNext()
            self.count -= 1
            return self.cs[self.pos]
    
        def hasNext(self):
            return self.count > 0 or self.nextPos < len(self.cs)
        
        # Utility function to count next digit substring    
        def countNext(self):
            self.nextPos = len(self.cs)
            for i in range(self.pos+1, len(self.cs)):
                if not self.cs[i].isdigit():
                    self.nextPos = i
                    break
            return int(self.cs[self.pos+1:self.nextPos])
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.