Number of Lines To Write String - Problem

Imagine you're designing a text editor that needs to wrap text across multiple lines based on pixel width rather than character count! ๐Ÿ“

You have a string s containing only lowercase English letters and an array widths where each element represents the pixel width of a letter. Specifically, widths[0] is the width of 'a', widths[1] is the width of 'b', and so on.

Your task is to write the string across multiple lines where each line can be at most 100 pixels wide. Start from the beginning of the string and pack as many characters as possible on each line without exceeding the 100-pixel limit.

Goal: Return an array of length 2 where:
โ€ข result[0] = total number of lines needed
โ€ข result[1] = width of the last line in pixels

For example, if writing "abc" with widths [4, 6, 8] and each character fits on one line, you'd need 1 line with total width 18 pixels.

Input & Output

example_1.py โ€” Basic Example
$ 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. Line 1 fits 10 chars (100px), Line 2 fits 10 chars (100px), Line 3 fits 6 chars (60px). Total: 3 lines, last line width 60.
example_2.py โ€” Single Line
$ 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 = "bbbcccccccccccccccccc"
โ€บ Output: [2, 4]
๐Ÿ’ก Note: Each 'b' has width 10, each 'c' has width 10. First line: 10 'c's (100px). Second line: 3 'b's + 1 'c' would exceed 100, so just 1 'b' (10px). Wait, let me recalculate: 'b'=10, 'c'=10. String has 1 'b' + 20 'c's. Line 1: 10 'c's (100px). Line 2: 1 'b' + 9 'c's (100px). Line 3: 1 'c' (10px). Actually result should be [3, 10]. Let me fix: First 'b'(10) + 9 'c's(90) = 100px on line 1. Then 11 'c's need 110px, so 10 'c's(100px) on line 2, 1 'c'(10px) on line 3. But the string is "bbbcccc...", so 3 'b's + 20 'c's. Line 1: 'b'+'b'+'b'+'c'+'c'+'c'+'c'+'c'+'c'+'c' = 70px. Line 2: remaining 13 'c's, but only 10 fit (100px). Line 3: 3 'c's (30px). Hmm, let me be more careful.
example_3.py โ€” Edge Case Single Character
$ 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 = "a"
โ€บ Output: [1, 4]
๐Ÿ’ก Note: Only one character 'a' with width 4. Fits on one line with total width 4.

Constraints

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

Visualization

Tap to expand
Text Editor Line Wrapping VisualizationString: "hello" with widths h=8, e=6, l=4, l=4, o=5 (total=27px)Text Editor View (100px line width):hello100px limitStep-by-Step Process:1width = 0, add 'h' (8px) โ†’ width = 8px โœ“2width = 8, add 'e' (6px) โ†’ width = 14px โœ“3width = 14, add 'l' (4px) โ†’ width = 18px โœ“4width = 18, add 'l' (4px) โ†’ width = 22px โœ“5width = 22, add 'o' (5px) โ†’ width = 27px โœ“Result:โ€ข Total lines needed: 1โ€ข Width of last line: 27 pixelsโ€ข Return: [1, 27]Complexity Analysis:โ€ข Time: O(n) - process each character onceโ€ข Space: O(1) - only use constant variablesโ€ข Optimal solution!๐Ÿ’ก Key Insight: Greedy approach works because we must process characters in order
Understanding the Visualization
1
Start Writing
Begin with line 1 and track current width
2
Add Characters
For each character, check if it fits on current line
3
Wrap Lines
When a character won't fit, start a new line
4
Complete
Return total lines and last line width
Key Takeaway
๐ŸŽฏ Key Insight: This problem has a naturally greedy solution because we must process characters sequentially and all lines have the same capacity constraint. There's no need to look ahead or try different arrangements - simply fill each line to maximum capacity before starting the next one.
Asked in
Google 15 Amazon 12 Microsoft 8 Adobe 6
24.6K Views
Medium Frequency
~12 min Avg. Time
892 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