#68
Text Justification
candidate master · 1485 · lc hard +32 · verified · 50.5% accepted · 4,521 likes · top 38%
Description
Arrange words into lines of exactly maxWidth characters by greedily filling each line. Distribute extra spaces between words as evenly as possible, assigning remainder spaces to leftmost gaps. The last line is left-justified with single spaces and right-padded. Each word is non-empty with length at most maxWidth; at least one word is present.
Example 1:
Input: words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16
Output:
[
"This is an",
"example of text",
"justification. "
]
Example 2:
Input: words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16
Output:
[
"What must be",
"acknowledgment ",
"shall be "
]
Explanation: Note that the last line is "shall be " instead of "shall be", because the last line must be left-justified instead of fully-justified.
Note that the second line is also left-justified because it contains only one word.
Example 3:
Input: words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"], maxWidth = 20
Output:
[
"Science is what we",
"understand well",
"enough to explain to",
"a computer. Art is",
"everything else we",
"do "
]
Code
1
2
3