You are given a list of words (strings with no spaces) and an integer maxWidth.
Implement text formatting in two variants:
Variant A — Basic word wrap (no padding)
Return lines by packing words greedily from left to right:
-
Each line contains as many words as possible.
-
Words in a line are separated by
a single space
.
-
Do not
pad the end of the line with extra spaces.
Variant B — Full justification (fixed width)
Return lines of exactly maxWidth characters:
-
Pack words greedily as in Variant A.
-
For every line
except the last
:
-
Distribute spaces between words so that the line length is exactly
maxWidth
.
-
If the spaces do not divide evenly, the
leftmost gaps
get more spaces.
-
If a line has only
one
word, left-justify it and pad trailing spaces to reach
maxWidth
.
-
For the
last
line:
-
Left-justify: words separated by a single space, then pad trailing spaces to reach
maxWidth
.
Input
-
words: List[str]
-
maxWidth: int
Output
-
List[str]
lines formatted according to the chosen variant.
Constraints (typical)
-
1 <= len(words) <= 10^4
-
1 <= len(words[i]) <= maxWidth
-
1 <= maxWidth <= 100