Hints — p97 Text Justification
-
Two phases: (a) greedy line-packing — decide how many words fit per line; (b) format each line according to rules.
-
Line fit condition:
line_len + 1 + len(next_word) <= maxWidth, whereline_len = sum(word lengths) + (count - 1) for mandatory spaces. -
Even-with-left-leaning distribution:
base, extra = divmod(maxWidth - total_word_chars, k - 1). Firstextragaps getbase + 1; rest getbase. -
Special cases: (a) last line → left-justify (single spaces + trailing pad). (b) single-word line → left-justify (no gaps to fill).
-
Verify by re-parsing output: each line must have length
maxWidthand the words in order must equal the input.
If stuck: see solution.py.