LeetCode 418, Medium. Topics: Array, String, Dynamic Programming. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 16 parametrized pytest cases, and a playground notebook:
Problem
Given a rows x cols screen and a sentence represented as a list of strings, return the number of times the given sentence can be fitted on the screen.
The order of words in the sentence must remain unchanged, and a word cannot be split into two lines. A single space must separate two consecutive words in a line.
Examples
Constraints
1 <= sentence.length
1 <= rows, cols <= 4 * 10^4
1 <= sentence[i].length <= 10
sentence[i] consists of only lower-case English letters.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026