Python straightforward no list no regexp


  • 0
    D
    class StringIterator(object):
    
        def __init__(self, compressedString):
            self.string = compressedString
            self.char = ' '
            self.remain = 0
            self.idx = 0
    
        def next(self):
            if not self.hasNext():
                return ' '
            self.remain -= 1
            return self.char
            
        def hasNext(self):
            if self.remain > 0:
                return True
            if self.idx >= len(self.string):
                return False
            self.char = self.string[self.idx]
            self.idx += 1
            num = ''
            while self.idx < len(self.string) and self.string[self.idx] >= '0' and self.string[self.idx] <= '9':
                num += self.string[self.idx]
                self.idx += 1
            self.remain = int(num)
            return True
    

Log in to reply
 

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