A newspaper prints articles in narrow columns where both edges are straight. Given the article
text and the column width, return the column as a list of lines. Every line must be
exactly width characters long.
- Words. The words of
textare the pieces separated by spaces (there may be several spaces in a row, or spaces at the ends). A word longer thanwidthis first cut into pieces ofwidthcharacters, left to right (the last piece may be shorter); each piece then counts as a word of its own. - Filling. Fill lines from left to right. A word goes on the current line if it fits with a single space before it; otherwise it starts a new line.
- Stretching. On every line except the last, widen the gaps between words until the line is
widthlong. All gaps get the same number of spaces, except that when the spaces do not divide evenly the gaps on the left get one extra space each. - Special lines. A line holding only one word, and the last line, keep single spaces between
words and are padded with spaces on the right up to
width.
If text has no words, return [].
Examples
Input: text = "the quick brown fox jumps over the lazy dog", width = 16
Output: ["the quick brown",
"fox jumps over",
"the lazy dog "]
Input: text = "a bb extraordinary", width = 6
Output: ["a bb",
"extrao",
"rdinar",
"y "]
Constraints
1 <= width <= 800 <= len(text) <= 5000;textcontains lowercase letters, punctuation and spaces only
Goals
- Pack words greedily into lines of a fixed width
- Share extra spaces between the gaps with a precise left-first rule
- Handle one-word lines, the last line and words too long for a line