Ctrl K

Text on a Narrow Display

Easy
A display shows lines no wider than 100 pixels. Each lowercase English letter has its own width: widths[0] is the width of a, widths[1] the width of b, and so on. The text text is laid out letter by letter from left to right: the next letter goes on the current line if the line stays within 100 pixels afterwards. Otherwise a new line starts and the letter becomes its first. There is no word wrapping; only letters matter. Return a list of two numbers: how many lines the text takes and how wide the last line is in pixels.
Example 1
widths=[20, 30, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10], text="abbac" → [2, 10]

a, b, b take 20 + 30 + 30 = 80 pixels; the next a (20) still fits, filling exactly 100. The letter c moves to a second line, which is 10 pixels wide.

Example 2
widths=[5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5], text="hello" → [1, 25]

Five letters of 5 pixels each make one line 25 pixels wide.

Constraints
  • len(widths) = 26
  • 1 ≤ widths[i] ≤ 100
  • 1 ≤ len(text) ≤ 1000
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo Words, Closest Apart EasyOne-Row Typing EasyDinner for Two EasyPhrases with Synonyms EasyWord from a License Plate Easy
loading Python…
Input
widths=[20, 30, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10], text="abbac"
Expected
[2, 10]

Run checks the first 3 tests in your browser. Submit checks all 48 tests on the server, hidden ones included.