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
Sentence Screen Fitting: ["hello", "world"] on 2×8 screenInput: ["hello", "world"]2×8 ScreenRow 1: hello woRow 2: rld hell8 chars each1 completesentence fitsOutput: 1
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
Asked in
Google 15 Amazon 12 Facebook 8 Microsoft 6
28.0K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen