All problems
0869EasyArrayString

Rows Needed to Set the Notice

Tracked in this browser only
Write code

Trains the technique from

LeetCode 806Number of Lines To Write String

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A typesetter fills rows exactly 100 units wide. Letter 'a' takes widths[0] units, letter 'b' takes widths[1], and so on through the alphabet.

The letters of s are set in order. A letter goes on the current row when it still fits within 100 units; otherwise a new row is started and the letter goes at its beginning. No letter is ever split across two rows.

Return a list of two numbers: how many rows were used, and how many units the last row holds.

Examples

Example 1

Input
widths = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10], s = "abcdefghijk"
Output
[2, 10]

Every letter takes 10 units, so ten of them exactly fill the first row and the eleventh starts a second row holding 10 units.

Example 2

Input
widths = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2], s = "a"
Output
[1, 2]

A single letter of width 2 sits on the first row, which then holds 2 units.

Example 3

Input
widths = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2], s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
Output
[2, 2]

Fifty letters of width 2 fill the first row exactly, so the fifty-first starts a second row.

Constraints

  • widths.length == 26
  • 2 <= widths[i] <= 10
  • 1 <= s.length <= 1000
  • s consists of lowercase English letters only

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def rows_needed(widths: list[int], s: str) -> list[int]:
Java
public int[] rowsNeeded(int[] widths, String s)
September 7
Apply