418 - Sentence Screen Fitting
Written on February 28, 2018
Tweet
Given a rows x cols screen and a sentence represented by a list of non-empty words, find how many times the given sentence can be fitted on the screen.
class Solution:
def wordsTyping(self, sentence: List[str], rows: int, cols: int) -> int:
new_s = " ".join(sentence) + " "
start = 0
for i in xrange(rows):
start += cols
if new_s[start % len(new_s)] == " ":
start += 1
else:
while start > 0 and new_s[(start - 1) % len(new_s)] != " ":
start -= 1
return start / len(new_s)