68. Text Justification
Practice Text Justification for free on NudgeCode: Hard, Array & Hash Table pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.
Lay out words into lines of exactly maxWidth characters and return the lines.
- Fill lines greedily: put as many words on a line as fit with at least one space between neighbours.
- Pad each line with extra spaces so it is exactly
maxWidthlong, spreading spaces between words as evenly as possible. If they don't divide evenly, the gaps on the left get one more space than the gaps on the right. - A line with a single word is left-justified (pad on the right).
- The last line is left-justified: one space between words, then pad on the right.
Example
words = ["This","is","an","example","of","text","justification."], maxWidth = 16
-> ["This is an",
"example of text",
"justification. "]
Constraints
- 1 <= words.length <= 300
- 1 <= words[i].length <= maxWidth <= 100
- Words consist of non-space characters