OA expected single quote string in Python, how to do that?


  • 0
    H

    The following code output double quote around string but OA expected single quote.
    How to out put single quote

    # input
    ["StringIterator","next","next","next","next","next","next","hasNext","next","hasNext"]
    [["L1e2t1C1o1d1e1"],[],[],[],[],[],[],[],[],[]]
    # output of following code
    [null,"L","e","e","t","C","o",true,"d",true]
    # expected output
    [null,'L','e','e','t','C','o',true,'d',true]
    
    class StringIterator(object):
    
        def __init__(self, compressedString):
            """
            :type compressedString: str
            """
            self.queue = []
            i = 0
            n = len(compressedString)
            while i < n:
                j = i + 1
                # get index of j that are not a digit
                while j < n and compressedString[j] in '0123456789':
                    j += 1
                # put [char, num] in queue
                self.queue.append([compressedString[i], int(compressedString[i + 1: j])])
                i = j
    
        def next(self):
            """
            :rtype: str
            """
            if not self.hasNext():
                return ' '
            t, n = self.queue[0]
            if n == 1:
                self.queue.pop(0)
            else:
                self.queue[0] = [t, n - 1]
            return t
    
        def hasNext(self):
            """
            :rtype: bool
            """
            return len(self.queue) != 0
    
    
            # Your StringIterator object will be instantiated and called as such:
            # obj = StringIterator(compressedString)
            # param_1 = obj.next()
            # param_2 = obj.hasNext()
    

Log in to reply
 

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