Text Justification
Problem statement
Given an array of non-empty strings words and an integer maxWidth, format the words into lines of exactly maxWidth characters.
Greedily place as many words as possible on each line. For every non-final line containing multiple words, distribute spaces as evenly as possible; when they do not divide evenly, gaps farther left receive one extra space. A non-final line containing one word is padded on the right.
The final line is left-justified with one space between adjacent words and trailing spaces added to reach maxWidth. Return the formatted lines.
Function
fullJustify(words: String[], maxWidth: int) → List<String>Examples
Example 1
words = ["This", "is", "an", "example", "of", "text", "justification."]maxWidth = 16return = ["This is an", "example of text", "justification. "]The greedy grouping produces three lines of exactly 16 characters. Spaces on non-final lines are distributed evenly, while the final line is padded on the right.
Example 2
words = ["What","must","be","acknowledgment","shall","be"]maxWidth = 16return = ["What must be", "acknowledgment ", "shall be "]The one-word middle line is padded on the right. The final line uses one space between its words and trailing spaces to reach width 16.
Example 3
words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"]maxWidth = 20return = ["Science is what we","understand well","enough to explain to","a computer. Art is","everything else we","do "]Each line has exactly 20 characters. Extra spaces on non-final lines are assigned to the leftmost gaps, and the final line is right-padded.
Constraints
1 <= words.length <= 3001 <= words[i].length <= 201 <= maxWidth <= 100words[i].length <= maxWidth- Every word contains only English letters and symbols.