806. Number of Lines To Write String Easy

@problem@discussion
#Array#String



1/**
2 * [806] Number of Lines To Write String
3 *
4 * 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.
5 * 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.
6 * Return an array result of length 2 where:
7 * 
8 * 	result[0] is the total number of lines.
9 * 	result[1] is the width of the last line in pixels.
10 * 
11 *  
12 * Example 1:
13 * 
14 * 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"
15 * Output: [3,60]
16 * Explanation: You can write s as follows:
17 * abcdefghij  // 100 pixels wide
18 * klmnopqrst  // 100 pixels wide
19 * uvwxyz      // 60 pixels wide
20 * There are a total of 3 lines, and the last line is 60 pixels wide.
21 * Example 2:
22 * 
23 * 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"
24 * Output: [2,4]
25 * Explanation: You can write s as follows:
26 * bbbcccdddaa  // 98 pixels wide
27 * a            // 4 pixels wide
28 * There are a total of 2 lines, and the last line is 4 pixels wide.
29 *  
30 * Constraints:
31 * 
32 * 	widths.length == 26
33 * 	2 <= widths[i] <= 10
34 * 	1 <= s.length <= 1000
35 * 	s contains only lowercase English letters.
36 * 
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/number-of-lines-to-write-string/
41// discuss: https://leetcode.com/problems/number-of-lines-to-write-string/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46    pub fn number_of_lines(widths: Vec<i32>, s: String) -> Vec<i32> {
47        vec![]
48    }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_806() {
59    }
60}
61


Back
© 2025 bowen.ge All Rights Reserved.