> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Sentence Screen Fitting Python Solution

> Tested Python solution for LeetCode 418 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 418, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [String](/catalog/topics/string), [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/sentence-screen-fitting/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 418   # by problem number
lcpy gen -s sentence_screen_fitting   # by problem name
```

## 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

```
Input: sentence = ["hello","world"], rows = 2, cols = 8
Output: 1
Explanation:
hello---
world---
The character '-' signifies an empty space on the screen.
```

```
Input: sentence = ["a", "bcd", "e"], rows = 3, cols = 6
Output: 2
Explanation:
a-bcd-
e-a---
bcd-e-
The character '-' signifies an empty space on the screen.
```

```
Input: sentence = ["i","had","apple","pie"], rows = 4, cols = 5
Output: 1
Explanation:
i-had
apple
pie-i
had--
The character '-' signifies an empty space on the screen.
```

### 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](https://github.com/wislertt/leetcode-py/blob/main/leetcode/sentence_screen_fitting/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/sentence_screen_fitting/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(rows + total_sentence_length)
    # Space: O(total_sentence_length) for the joined sentence string
    def words_typing(self, sentence: list[str], rows: int, cols: int) -> int:
        s = " ".join(sentence) + " "
        n = len(s)
        total = 0
        for _ in range(rows):
            total += cols
            if s[total % n] == " ":
                total += 1
            else:
                while total > 0 and s[(total - 1) % n] != " ":
                    total -= 1
        return total // n
```

## Complexity

| Time | Space |
| - | - |
| O(rows + total\_sentence\_length) | O(total\_sentence\_length) for the joined sentence string |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.