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.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 <= 1000s contains only lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach involves testing every possible combination of words on each line to find a valid arrangement. It exhaustively explores all ways to split the given text into lines that meet the maximum line width. We continue until we've considered all possible line arrangements.
Here's how the algorithm would work step-by-step:
def number_of_lines_brute_force(
widths,
text,
max_width
):
words = text.split()
minimum_lines = float('inf')
for start_index in range(len(words)):
number_of_lines = 0
current_line_width = 0
word_index = start_index
# Iterate to build lines.
while word_index < len(words):
number_of_lines += 1
current_line_width = 0
while word_index < len(words):
word_width = widths[ord(words[word_index]) - ord('a')]
# Check if adding the next word exceeds max width
if current_line_width + word_width > max_width:
break
current_line_width += word_width
word_index += 1
minimum_lines = min(minimum_lines, number_of_lines)
return minimum_linesThe challenge is to determine how many lines are needed to write a string given a specific width for each character. The optimal approach is to iterate through the string, character by character, and keep track of the current line's used width. When the line is full, increment the line count and start a new line.
Here's how the algorithm would work step-by-step:
def number_of_lines(widths, string_to_write):
line_count = 1
current_line_width = 0
for character in string_to_write:
character_index = ord(character) - ord('a')
character_width = widths[character_index]
# Check if adding the character exceeds the line limit.
if current_line_width + character_width > 100:
line_count += 1
# Start a new line with the current character's width.
current_line_width = character_width
else:
# Add the character's width to the current line.
current_line_width += character_width
return [line_count, current_line_width]| Case | How to Handle |
|---|---|
| Null or empty input widths array | Return [0, 0] indicating 0 lines and 0 pixels used on the last line, as there is nothing to write. |
| Input string S is null or empty | Return [0, 0] indicating 0 lines and 0 pixels used on the last line, as there is nothing to write. |
| All characters in S require maximum width (e.g., all 'w') | Handle line wrapping correctly; ensure line count increments when exceeding 100 pixels. |
| All characters in S require minimum width (e.g., all 'a') | Multiple characters will fit on each line, correctly incrementing character count and line count only when needed. |
| Widths array contains a zero value | The problem statement specifies widths[i] to be non-zero, and any input violating this constraint means the input is malformed and an exception should be thrown or a designated error value returned. |
| Input string 'S' contains characters not present in the widths array (e.g., extended ASCII or Unicode) | Throw an IllegalArgumentException, return a designated error value like [-1, -1], or handle gracefully by skipping those characters if within a defined subset. |
| The String 'S' is extremely long (close to Integer.MAX_VALUE characters) | Handle potential integer overflow when calculating pixel usage, especially if character widths are large; consider using long to store the total pixels. |
| Widths array contains extremely large values (approaching Integer.MAX_VALUE), but the total pixels do not exceed Integer.MAX_VALUE due to short string length | Ensure intermediate pixel calculations don't overflow before final checks with the widths array. |