You are given an array widths
of length 26, where widths[i]
represents the width of the lowercase English letter chr(i + ord('a'))
. You are also given a string S
.
You need to write the string S
on lines such that each line can contain at most 100 units of width. You must write characters in order, and you cannot split a character between lines.
Return an array or list containing two integers:
S
is a lowercase letter.At first glance, the problem seems to ask for a simple simulation: write the string character by character, keeping track of the current line's width, and starting a new line whenever adding the next character would exceed the maximum width (100 units).
The brute-force approach would be to scan through the string, for each character:
Let's break down the algorithm step-by-step:
lines = 1
(since at least one line is needed) and curr_width = 0
to track the width used on the current line.
S
:
widths
array. For example, widths[ord(char) - ord('a')]
.curr_width + char_width > 100
, increment lines
and reset curr_width
to the current character's width (since it starts the new line).char_width
to curr_width
.[lines, curr_width]
, where curr_width
is the width used on the last line.
This approach is efficient and simple because:
O(n)
time).
Example:
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"
Let's trace the process:
[3, 60]
Brute-force approach:
Even the brute-force approach here is efficient, since we process each character once.
O(n)
, where n
is the length of S
.O(1)
, only a few variables are used for tracking lines and width.This problem is a straightforward simulation: write out the string character by character, keeping track of the current line's width and moving to a new line as needed. The key insight is to process each character in order, using a simple counter for the line width and number of lines. This makes for an elegant, efficient, and easy-to-understand solution.
class Solution:
def numberOfLines(self, widths, S):
lines = 1
curr_width = 0
for c in S:
w = widths[ord(c) - ord('a')]
if curr_width + w > 100:
lines += 1
curr_width = w
else:
curr_width += w
return [lines, curr_width]
class Solution {
public:
vector<int> numberOfLines(vector<int>& widths, string S) {
int lines = 1, curr_width = 0;
for (char c : S) {
int w = widths[c - 'a'];
if (curr_width + w > 100) {
lines++;
curr_width = w;
} else {
curr_width += w;
}
}
return {lines, curr_width};
}
};
class Solution {
public int[] numberOfLines(int[] widths, String S) {
int lines = 1, curr_width = 0;
for (char c : S.toCharArray()) {
int w = widths[c - 'a'];
if (curr_width + w > 100) {
lines++;
curr_width = w;
} else {
curr_width += w;
}
}
return new int[]{lines, curr_width};
}
}
var numberOfLines = function(widths, S) {
let lines = 1, curr_width = 0;
for (let i = 0; i < S.length; ++i) {
let w = widths[S.charCodeAt(i) - 'a'.charCodeAt(0)];
if (curr_width + w > 100) {
lines++;
curr_width = w;
} else {
curr_width += w;
}
}
return [lines, curr_width];
};