Justify Log Line Wrapping
A monospace console pane must fully justify wrapped text to a fixed column width: each line packs as many words as fit, and extra spaces are spread between words (left-biased). The final line is left-justified.
Problem
Given an array words and an integer maxWidth, format the text so each line is exactly maxWidth characters and fully (left-and-right) justified. Pack greedily; distribute extra spaces between words with earlier gaps getting more when uneven. The last line is left-justified with a single space between words and padded on the right.
Input
An array words of strings and an integer maxWidth (each word length ≤ maxWidth ≤ 100).
Output
A list of strings, each exactly maxWidth characters long.
Constraints
- 1 ≤ words.length ≤ 300
- Each word fits within maxWidth
- Lines (except the last and single-word lines) are fully justified
Examples
Example 1
Input
words = ["This","is","an","example","of","text","justification."], maxWidth = 16
Output
["This is an","example of text","justification. "]
Earlier gaps absorb the extra space; the last line is left-justified.
Example 2
Input
words = ["What","must","be"], maxWidth = 12
Output
["What must","be "]
Two words split the slack; the final word's line is left-justified.