|
| 1 | +806\. Number of Lines To Write String |
| 2 | + |
| 3 | +Easy |
| 4 | + |
| 5 | +You are given a string `s` of lowercase English letters and an array `widths` denoting **how many pixels wide** each lowercase English letter is. Specifically, `widths[0]` is the width of `'a'`, `widths[1]` is the width of `'b'`, and so on. |
| 6 | + |
| 7 | +You are trying to write `s` across several lines, where **each line is no longer than** `100` **pixels**. Starting at the beginning of `s`, write as many letters on the first line such that the total width does not exceed `100` pixels. Then, from where you stopped in `s`, continue writing as many letters as you can on the second line. Continue this process until you have written all of `s`. |
| 8 | + |
| 9 | +Return _an array_ `result` _of length 2 where:_ |
| 10 | + |
| 11 | +* `result[0]` _is the total number of lines._ |
| 12 | +* `result[1]` _is the width of the last line in pixels._ |
| 13 | + |
| 14 | +**Example 1:** |
| 15 | + |
| 16 | +**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 = "abcdefghijklmnopqrstuvwxyz" |
| 17 | + |
| 18 | +**Output:** [3,60] |
| 19 | + |
| 20 | +**Explanation:** You can write s as follows: |
| 21 | + |
| 22 | +abcdefghij // 100 pixels wide |
| 23 | + |
| 24 | +klmnopqrst // 100 pixels wide |
| 25 | + |
| 26 | +uvwxyz // 60 pixels wide |
| 27 | + |
| 28 | +There are a total of 3 lines, and the last line is 60 pixels wide. |
| 29 | + |
| 30 | +**Example 2:** |
| 31 | + |
| 32 | +**Input:** widths = [4,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 = "bbbcccdddaaa" |
| 33 | + |
| 34 | +**Output:** [2,4] |
| 35 | + |
| 36 | +**Explanation:** You can write s as follows: |
| 37 | + |
| 38 | +bbbcccdddaa // 98 pixels wide |
| 39 | + |
| 40 | +a // 4 pixels wide |
| 41 | + |
| 42 | +There are a total of 2 lines, and the last line is 4 pixels wide. |
| 43 | + |
| 44 | +**Constraints:** |
| 45 | + |
| 46 | +* `widths.length == 26` |
| 47 | +* `2 <= widths[i] <= 10` |
| 48 | +* `1 <= s.length <= 1000` |
| 49 | +* `s` contains only lowercase English letters. |
0 commit comments