Leetcode Problem 806. Number of Lines To Write String

806. Number of Lines To Write String

Leetcode Solutions

Iterative Line-by-Line Writing

  1. Initialize lines to 1 and width to 0.
  2. Iterate through each character in the string s.
  3. For each character, find its corresponding width from the widths array using its ASCII value.
  4. Check if adding this character's width to the current width exceeds 100 pixels.
  5. If it does, increment lines and set width to the current character's width.
  6. If it doesn't, add the character's width to the current width.
  7. After iterating through all characters, return [lines, width] as the result.
UML Thumbnail

Cumulative Width Calculation with Line Breaks

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...