Text Justification
Given an array of words and a width maxWidth, format the text so each line has exactly maxWidth characters and is fully justified. Pack as many words per line as fit, distribute extra spaces as evenly as possible with larger gaps assigned to the left, and left-justify the final line and any line with a single word.
Open official problem prompt ↗Lay out a stream of words into fixed-width, fully justified lines matching a newspaper column, with the last line left-aligned.
A typesetter fills each column line with words, then stretches the gaps between them like an accordion so both margins line up flush.
- Input
- words = ["This","is","an","example","of","text","justification."], maxWidth = 16
- Output
- ["This is an","example of text","justification. "]
- Why
- Lines 1-2 spread spaces evenly (left gaps get the extra), and the last line is left-justified then padded to width 16.
1 <= words.length <= 3001 <= words[i].length <= 20words[i] consists of only English letters and symbols1 <= maxWidth <= 100words[i].length <= maxWidth