Sentence Screen Fitting
Input:
rows = 2, cols = 8, sentence = ["hello", "world"]
Output:
1
Explanation:
hello---
world---
The character '-' signifies an empty space on the screen.Last updated
Input:
rows = 2, cols = 8, sentence = ["hello", "world"]
Output:
1
Explanation:
hello---
world---
The character '-' signifies an empty space on the screen.Last updated
Input:
rows = 3, cols = 6, sentence = ["a", "bcd", "e"]
Output:
2
Explanation:
a-bcd-
e-a---
bcd-e-
The character '-' signifies an empty space on the screen.Input:
rows = 4, cols = 5, sentence = ["I", "had", "apple", "pie"]
Output:
1
Explanation:
I-had
apple
pie-I
had--
The character '-' signifies an empty space on the screen.class Solution:
def wordsTyping(self, sentence: List[str], rows: int, cols: int) -> int:
s = ' '.join(sentence)+' '
ln = len(s)
start = 0
for i in range(rows):
start += cols
if s[start %ln] == ' ': #可以塞入当前row,不用padding
start += 1
else:#去掉最后一个word,去下一行
while start > 0 and s[(start-1)%ln] != ' ':#loop出来start在某word第一个字母处
start -= 1
return start//ln