Simple Python with Regex and Iterator


  • 0
    W
    class StringIterator(object):
    
        def __init__(self, compressedString):
            """
            :type compressedString: str
            """
            l = re.findall('[a-zA-Z]\d+', compressedString)
            self.total = sum(int(c[1:]) for c in l)
            self.i = -1
            
            def ite(l):
                for c in l:
                    for i in xrange(int(c[1:])):
                        yield c[0]
            self.it = ite(l)
        
        def next(self):
            """
            :rtype: str
            """
            self.i += 1
            return next(self.it, " ")
    
        def hasNext(self):
            """
            :rtype: bool
            """
            return self.i < self.total - 1
    

Log in to reply
 

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