You are given a list of words and a line width maxWidth. Lay the words out as lines of text where every line is exactly maxWidth characters long, and return the lines.
Fill lines greedily: put as many words on a line as fit with at least one space between neighbours. Then spread the line's leftover spaces across its gaps as evenly as possible; when they do not split evenly, the gaps further left get the extra ones.
Two kinds of line are left-justified instead: the final line, and any line holding a single word. Their words are separated by one space and the rest of the line is filled with spaces on the right.
Example 1
Input: words = ["Cats", "sit", "on", "warm", "mats", "all", "day."],
maxWidth = 14
Output: ["Cats sit on",
"warm mats all",
"day. "]
Why: 5 spare spaces split 3 + 2, the left gap gets the extra one2 more examples and the constraints
Example 2
Input: words = ["Go", "extraordinary", "now"], maxWidth = 13
Output: ["Go ",
"extraordinary",
"now "]
Why: a line with one word is padded on the rightExample 3
Input: words = ["one", "two", "three", "four"], maxWidth = 10
Output: ["one two",
"three four"]
Why: the last line keeps single spacesConstraints
- 1 ≤ number of words ≤ 300
- 1 ≤ length of each word ≤ 20, and no word is longer than maxWidth
- 1 ≤ maxWidth ≤ 100
- Words contain no spaces
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Pack, then padO(n · W)
- One pass with divmodO(n · W)
The idea in one sentence
Side by side
| Approach | Time | Relative work for 300 words, width 100 | Verdict |
|---|---|---|---|
| Pack, then pad | O(n · W) | Passes; two passes | |
| One pass with divmod | O(n · W) | Best; the split in one step |
n = number of words, W = maxWidth. The output itself has up to n · W characters.
Edge cases to test
- One word on a line. There are no gaps to share, so it is padded on the right, even when it is not the last line.
- Last line. Single spaces between words, then spaces up to the width. Never stretched.
- Word as long as the width. It fills its line alone with no padding at all.
- Spaces that do not split evenly. Extras go to the leftmost gaps: 7 spare spaces in 3 gaps is 3, 2, 2.
- Width 1. Every word has one letter and sits on its own line.
Common mistakes
- Forgetting the one-space-per-gap minimum while packing, so too many words land on a line.
- Stretching the last line like the others, or not padding it to the full width.
- Dividing by the number of gaps when a line has a single word, which divides by zero.
How to talk through it
- 1Clarify. Is packing greedy? Where do uneven extras go? How are the last line and one-word lines handled?
- 2Describe the packing. Add words while letters plus one space per gap fit in the width.
- 3Do the arithmetic. Spare spaces divided by gaps gives the base; the remainder goes one each to the leftmost gaps.
- 4Handle the special lines. Last line and single-word lines: join with one space, pad the right.
- 5Test out loud. Check one uneven line, one single-word line and the last line, and that every line has the full width.