Text Justification
Problem statement
Given an ordered array of nonempty strings words and an integer maxWidth, return lines of exactly maxWidth characters.
For this exercise, use the following formatting rules:
- Keep the words in input order and never split a word. Put as many consecutive words as possible on each line, allowing at least one space between adjacent words.
- For each nonfinal line with two or more words, distribute all spaces as evenly as possible between words. When some gaps need an extra space, give it to the gaps farther left.
- Left-align the final line and every line containing only one word: use one space between adjacent words, then add trailing spaces to reach
maxWidth.
Trailing spaces are part of the returned strings.
Function
fullJustify(words: String[], maxWidth: int) → List<String>Examples
Example 1
words = ["a","bb","ccc","dddd","ee"]maxWidth = 10return = ["a bb ccc","dddd ee "]The first line contains six word characters and four spaces, split into two gaps. The final line has one internal space and three trailing spaces.
Example 2
words = ["longword","a","b"]maxWidth = 8return = ["longword","a b "]The first word fills its line. The final line contains three characters before five trailing spaces.
Example 3
words = ["hi"]maxWidth = 4return = ["hi "]The single final word receives two trailing spaces.
Constraints
- For this exercise, assume
1 <= words.length <= 200and1 <= maxWidth <= 80. - Each word contains printable ASCII characters without spaces.
- For this exercise, assume
1 <= words[i].length <= min(20, maxWidth).