↓Skip to main content
  1. LeetCode/

LeetCode 806: Number of Lines To Write String

·2 mins· ·
LeetCode Easy Array String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-07

  • Submissions: Wrong Answer → Runtime Error → Wrong Answer ×6 → Runtime Error → Wrong Answer ×2 → Accepted

Approach
#

Simulate from line 1 at width 0: wrap only when the current width plus this letter’s width exceeds 100 (the new line starts at this letter’s width); otherwise add it. Reaching exactly 100 stays on the line.

My Attempts
#

This took 12 submissions, all patching one wrong idea, “exactly 100 moves to the next line at width 0”:

  • Attempt 1 used the letter’s index as its width instead of looking up widths, and a lone "a" was then reduced by the trailing “width 0 means one line fewer” fix to [-1, 0].
  • After wrapping, the width was set to the letter’s width and then added again by the next if: [2, 23] instead of [2, 15].
  • With elif, filling exactly 100 was treated as a wrap and then undone, giving [1, 0] instead of [1, 100].
  • The accepted version patched the end: “if the width is 0, drop a line and set the width to 100”.

Solution
#

class Solution:
    def numberOfLines(self, widths: List[int], s: str) -> List[int]:
        lines, cur = 1, 0
        for c in s:
            w = widths[ord(c) - ord("a")]
            if cur + w > 100:
                lines += 1
                cur = w
            else:
                cur += w
        return [lines, cur]

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Exactly 100 does not wrap; the last line’s width can be 100.
  • After wrapping the new line holds this letter’s width; do not add it again.
  • Look the width up in widths, not the letter’s index.

Interview Explanation
#

I simulate from the first line, wrapping only when the next letter would exceed 100 and starting the new line at that letter’s width, otherwise accumulating; I return the line count and the last width. Time O(n).

Related

LeetCode 748: Shortest Completing Word
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 748 note: count the plate’s letters in lowercase, then find the shortest word covering every count, taking the earliest on ties
LeetCode 599: Minimum Index Sum of Two Lists
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 599 note: map list1’s strings to indices, sum both indices while scanning list2, and keep every string with the minimum sum
LeetCode 14: Longest Common Prefix
·2 mins
LeetCode Easy Array String Trie
LeetCode 14 note: bound by the shortest string and check each position across all strings; the first mismatch gives the answer’s length
LeetCode 804: Unique Morse Code Words
·1 min
LeetCode Easy Array Hash-Table String
LeetCode 804 note: translate each word into a Morse string and count the distinct results with a set
LeetCode 500: Keyboard Row
·1 min
LeetCode Easy Array Hash-Table String
LeetCode 500 note: make a set for each keyboard row and keep words whose lowercase character set is a subset of one row
LeetCode 796: Rotate String
·2 mins
LeetCode Easy String String-Matching
LeetCode 796 note: goal is a rotation of s exactly when the lengths match and goal occurs in s + s