Sentence Screen Fitting - Problem
Given a rows × 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.
Constraints:
- A word can only be placed on a line if it fits completely
- Words must appear in order
- Each line can contain multiple words separated by single spaces
- Count how many complete sentences fit on the screen
Input & Output
Example 1 — Basic Case
$
Input:
sentence = ["hello", "world"], rows = 2, cols = 8
›
Output:
1
💡 Note:
Row 1: "hello wo" (8 chars), Row 2: "rld hello" (9 chars, but only 8 fit: "rld hell"). One complete sentence "hello world" fits.
Example 2 — Multiple Sentences
$
Input:
sentence = ["a", "bcd", "e"], rows = 3, cols = 6
›
Output:
2
💡 Note:
Row 1: "a bcd e" (7 chars, fits as "a bcd "), Row 2: "e a bcd" (7 chars, fits as "e a bc"), Row 3: "d e a b" (7 chars, fits as "d e a "). Two complete sentences fit.
Example 3 — Single Long Word
$
Input:
sentence = ["hello-world"], rows = 1, cols = 10
›
Output:
0
💡 Note:
The word "hello-world" has 11 characters but column limit is 10, so it cannot fit at all.
Constraints
- 1 ≤ sentence.length ≤ 100
- 1 ≤ sentence[i].length ≤ 80
- sentence[i] consists of only lowercase English letters
- 1 ≤ rows, cols ≤ 2 × 104
Visualization
Tap to expand
Understanding the Visualization
1
Input
Sentence ["hello", "world"] and 2×8 screen
2
Process
Place words row by row with proper spacing
3
Output
Count complete sentences that fit
Key Takeaway
🎯 Key Insight: Track word positions across rows to count complete sentence cycles efficiently
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code