Hints — p97 Text Justification

  1. Two phases: (a) greedy line-packing — decide how many words fit per line; (b) format each line according to rules.

  2. Line fit condition: line_len + 1 + len(next_word) <= maxWidth, where line_len = sum(word lengths) + (count - 1) for mandatory spaces.

  3. Even-with-left-leaning distribution: base, extra = divmod(maxWidth - total_word_chars, k - 1). First extra gaps get base + 1; rest get base.

  4. Special cases: (a) last line → left-justify (single spaces + trailing pad). (b) single-word line → left-justify (no gaps to fill).

  5. Verify by re-parsing output: each line must have length maxWidth and the words in order must equal the input.

If stuck: see solution.py.