Text Justification
HardP3Coding
Format words into lines of exactly maxWidth characters, fully justified, and return the lines.
Pack as many words as fit on each line (words separated by at least one space). Then:
- For a line that is not the last and has two or more words, distribute extra spaces between words as evenly as possible. If the spaces do not divide evenly, the gaps on the left get one more space than the gaps on the right.
- The last line, and any line with a single word, is left-justified: single spaces between words, and the remaining spaces added at the end.
Every word is at most maxWidth characters long.
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 "]
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 "]
Constraints
- 1 <= words.length <= 300
- 1 <= words[i].length <= 20
- words[i] contains no spaces.
- words[i].length <= maxWidth <= 100
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 12. ⌘Enter runs, ⌘⇧Enter submits.