Count how many times a sentence fits on a rows by cols screen without splitting words

Quick Overview

Given a list of words and a screen with a fixed number of rows and columns, count how many complete copies of the sentence fit when words are never split across lines. Tests simulation over a repeating string, cursor arithmetic at row boundaries and handling words longer than a row.

Count how many times a sentence fits on a rows by cols screen without splitting words

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a screen of `rows` by `cols` cells and a sentence represented as a list of non-empty words, find how many times the sentence can be fitted on the screen. A word cannot be split across two lines. The order of the words in the sentence must stay unchanged, and two consecutive words on the same line must be separated by a single space. The sentence does not need to end with a space at the end of a line. ### Function Signature ```python def count_sentence_fits(sentence: list[str], rows: int, cols: int) -> int: ``` ### Rules - Words are written in order, row by row. After the last word of the sentence, writing continues with the first word again. - A row takes as many whole words as fit in `cols` cells, with one space between consecutive words on that row. A word that does not fit in the remaining cells starts the next row. - No space is needed before the first word of a row or after the last word of a row. - Return the number of complete copies of the sentence written on the screen. A partial copy at the end does not count. ### Constraints - `1 <= len(sentence) <= 100` - `1 <= len(sentence[i]) <= 10` - `sentence[i]` consists of lowercase English letters. - `1 <= rows, cols <= 20000` ### Examples ```text Input: sentence = ["hello", "world"], rows = 2, cols = 8 Output: 1 ``` Row 1 is `hello---` and row 2 is `world---`, where each `-` is an unused cell. The word "world" cannot follow "hello" on row 1 because "hello world" needs 11 cells, so the screen holds the sentence once. ```text Input: sentence = ["a", "bcd", "e"], rows = 3, cols = 6 Output: 2 ``` Row 1 is `a-bcd-`, row 2 is `e-a---` and row 3 is `bcd-e-`. The first copy ends with the "e" on row 2 and the second copy ends with the "e" on row 3, so the answer is 2. ```text Input: sentence = ["abcdef", "x"], rows = 5, cols = 4 Output: 0 ``` The word "abcdef" has 6 letters and a row has only 4 cells, so it never fits and no copy of the sentence can be completed.

Overview: Given a list of words and a screen with a fixed number of rows and columns, count how many complete copies of the sentence fit when words are never split across lines. Tests simulation over a repeating string, cursor arithmetic at row boundaries and handling words longer than a row.

Google logo
Google
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a screen of rows by cols cells and a sentence represented as a list of non-empty words, find how many times the sentence can be fitted on the screen.

A word cannot be split across two lines. The order of the words in the sentence must stay unchanged, and two consecutive words on the same line must be separated by a single space. The sentence does not need to end with a space at the end of a line.

Function Signature

def count_sentence_fits(sentence: list[str], rows: int, cols: int) -> int:

Rules

  • Words are written in order, row by row. After the last word of the sentence, writing continues with the first word again.
  • A row takes as many whole words as fit in cols cells, with one space between consecutive words on that row. A word that does not fit in the remaining cells starts the next row.
  • No space is needed before the first word of a row or after the last word of a row.
  • Return the number of complete copies of the sentence written on the screen. A partial copy at the end does not count.

Constraints

  • 1 <= len(sentence) <= 100
  • 1 <= len(sentence[i]) <= 10
  • sentence[i] consists of lowercase English letters.
  • 1 <= rows, cols <= 20000

Examples

Input: sentence = ["hello", "world"], rows = 2, cols = 8
Output: 1

Row 1 is hello--- and row 2 is world---, where each - is an unused cell. The word "world" cannot follow "hello" on row 1 because "hello world" needs 11 cells, so the screen holds the sentence once.

Input: sentence = ["a", "bcd", "e"], rows = 3, cols = 6
Output: 2

Row 1 is a-bcd-, row 2 is e-a--- and row 3 is bcd-e-. The first copy ends with the "e" on row 2 and the second copy ends with the "e" on row 3, so the answer is 2.

Input: sentence = ["abcdef", "x"], rows = 5, cols = 4
Output: 0

The word "abcdef" has 6 letters and a row has only 4 cells, so it never fits and no copy of the sentence can be completed.