Class Solution
- java.lang.Object
-
- g0801_0900.s0806_number_of_lines_to_write_string.Solution
-
public class Solution extends Object
806 - Number of Lines To Write String.Easy
You are given a string
sof lowercase English letters and an arraywidthsdenoting 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.You are trying to write
sacross several lines, where each line is no longer than100pixels. Starting at the beginning ofs, write as many letters on the first line such that the total width does not exceed100pixels. Then, from where you stopped ins, continue writing as many letters as you can on the second line. Continue this process until you have written all ofs.Return an array
resultof length 2 where:result[0]is the total number of lines.result[1]is the width of the last line in pixels.
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 = “abcdefghijklmnopqrstuvwxyz”
Output: [3,60]
Explanation: You can write s as follows:
abcdefghij // 100 pixels wide
klmnopqrst // 100 pixels wide
uvwxyz // 60 pixels wide
There are a total of 3 lines, and the last line is 60 pixels wide.
Example 2:
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”
Output: [2,4]
Explanation: You can write s as follows:
bbbcccdddaa // 98 pixels wide
a // 4 pixels wide
There are a total of 2 lines, and the last line is 4 pixels wide.
Constraints:
widths.length == 262 <= widths[i] <= 101 <= s.length <= 1000scontains only lowercase English letters.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
-
-
Method Detail
-
numberOfLines
public int[] numberOfLines(int[] widths, String s)
-
-