Hard · Strings

Text Justification

Given an array of words and a width maxWidth, return the text as lines of exactly maxWidth characters: put as many words on each line as fit with at least one space between them, spread each line's spaces as evenly as possible with the extra spaces in the leftmost gaps, and left-justify the last line and any one-word line by padding spaces on the right.

Examples

Example 1

["What","must","be","done"] · w=10

Output: ["What must","be done "]

Example 2

["a","b","c","d","e"] · w=5

Output: ["a b c","d e "]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Strings problems