9 lines in Python


  • 4
    S

    Re: 21ms 18 lines Java solution

    def wordsTyping(self, sentence, rows, cols):
            s = ' '.join(sentence)+' '
            start, l = 0,len(s)
            for i in xrange(rows):
                start+=cols
                while s[start % l]!=' ':
                    start-=1
                start+=1
            return start/l
    
    

  • 0

    Uglier, but hopefully it could be faster:

        def wordsTyping(self, sentence, rows, cols):
            s = ' '.join(sentence) + ' '
            start, l = 0, len(s)
            offsets = {i: cols + 1 - len(s[:i+1].split(' ')[-1]) for i in range(l)}
            for _ in range(rows):
                start += offsets[(start + cols) % l]
            return start // l
    

Log in to reply
 

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