Number of Lines To Write String - Problem

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.

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.

Return an array result of length 2 where:

  • result[0] is the total number of lines
  • result[1] is the width of the last line in pixels

Input & Output

Example 1 — Basic Case
$ 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]
💡 Note: All characters have width 10. First line fits 10 chars (100px), second line fits 10 chars (100px), third line has 6 chars (60px). Result: 3 lines, last line 60px.
Example 2 — Different Widths
$ 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]
💡 Note: b=10px, c=10px, d=10px, a=4px. First line: bbbcccddda (94px). Adding another 'a' would make 98px ≤ 100px, so 'aa' fits. Result: 2 lines, last line 4px.

Constraints

  • widths.length == 26
  • 2 ≤ widths[i] ≤ 10
  • 1 ≤ s.length ≤ 1000
  • s contains only lowercase English letters

Visualization

Tap to expand
Number of Lines To Write String INPUT widths[] - all letters = 10px a=10, b=10, c=10, d=10... ...w=10, x=10, y=10, z=10 (26 letters, each 10px wide) String s: "abcdefghijklmnopqrstuvwxyz" 26 characters total a b c ... x y z Constraint: Max line width = 100 pixels Input Values: widths = [10,10,...,10] (26 tens) s = "abcdefghij...xyz" ALGORITHM STEPS 1 Initialize lines=1, currentWidth=0 2 Iterate String For each char, get width 3 Check Line Fit If current+w > 100: new line 4 Return Result [lines, currentWidth] Line Filling Process: Line 1: a-j (10 chars) = 100px Line 2: k-t (10 chars) = 100px Line 3: u-z (6 chars) = 60px 10 + 10 + 6 = 26 chars total Last line width = 6 x 10 = 60px FINAL RESULT Written Lines: abcdefghij [100px - FULL] klmnopqrst [100px - FULL] uvwxyz [60px] Last line Output: [3, 60] result[0] = 3 lines result[1] = 60 pixels OK Solution verified! Key Insight: Single pass approach: Track current line width and increment line count when adding a character would exceed 100px. Time: O(n) where n = length of string. Space: O(1) - only tracking lines and currentWidth variables. TutorialsPoint - Number of Lines To Write String | Single Pass with Early Tracking
Asked in
Google 25 Facebook 20
32.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen