InterviewPrepKit

Home / Coding / Arrays & Hashing

Text Justification

hard Original ↗
Solving tips
  • This is a greedy simulation, not an optimization: decompose each line into two independent phases — pick which words fit, then space them — to avoid bugs.
  • Fit test: a line already holding k words accepts the next word iff sum(lengths) + len(word) + k <= maxWidth (k mandatory spaces).
  • Distribute the budget maxWidth-sum(lengths) across g gaps with divmod: every gap gets q, and the leftmost r gaps get one extra; a round-robin dealing loop achieves the same left-bias.
  • Pitfall: the last line AND any single-word line are left-justified (single spaces, right-padded) — don't fully-justify them; extra spaces go to the leftmost gaps.

Problem

You are given a list of words (non-empty strings of visible characters) and an integer maxWidth. Lay the words out into lines of text so that every line is exactly maxWidth characters long, and return the lines as a list of strings.

Formatting rules:

  • Fill lines greedily: put as many words on each line as will fit, keeping at least one space between adjacent words. Words must stay in their original order and may not be split.
  • A finished line (except the last) must be fully justified: pad the gaps between words with extra spaces so the line is exactly maxWidth wide. Spread the spaces as evenly as possible; when they cannot be divided evenly, the leftmost gaps receive the extra spaces.
  • A line containing only one word is left-justified: the word, then spaces to fill the width.
  • The final line is left-justified: single spaces between words, then trailing spaces to fill the width.

You may assume every word’s length is at most maxWidth, so any word fits on a line by itself.

Examples

Example 1 Input: words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16 Output: ["This is an", "example of text", "justification. "] Three words fit on each of the first two lines; extra spaces go to the left gaps first, and the last line is left-justified and padded on the right.

Example 2 Input: words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16 Output: ["What must be", "acknowledgment ", "shall be "] "acknowledgment" cannot share a line with anything, and a one-word line is left-justified, not centered.

Example 3 Input: words = ["Listen"], maxWidth = 10 Output: ["Listen "] A single word is also the last line, so it is left-justified and padded to width 10.

Constraints

  • 1 <= words.length <= 300
  • 1 <= words[i].length <= 20 and words[i].length <= maxWidth
  • 1 <= maxWidth <= 100

The input is tiny — this problem is not about asymptotic complexity but about a bug-free greedy simulation.

Think about it first

Hint 1 Solve it one line at a time. First decide *which* words go on the current line, then — as a completely separate step — decide how to space them. Mixing the two steps is where most bugs come from.
Hint 2 While packing a line, a candidate set of words fits if `sum(word lengths) + (number of gaps) <= maxWidth`, because each gap needs at least one space. If your line already holds `k` words, adding the next word costs its length plus one more mandatory space.
Hint 3 Once a line's words are fixed, the total space budget is `maxWidth - sum(word lengths)`. With `g` gaps, every gap gets `budget // g` spaces and the leftmost `budget % g` gaps get one extra. Handle two special cases separately: a line with a single word (no gaps) and the final line — both are left-justified with single spaces and right-padded.
Your workspace Not runnable by design — this is your interview scratchpad. Saved on this device.