Hard · Strings

Text Justification

Given an array of ASCII words, each no longer than 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

Core interview problems are free with a full runner and lessons; Pro opens the complete library.

More Strings problems