InterviewPrepKit

Home / Coding / Algorithm & Data Structure / Arrays & Hashing

Text Justification

hard Original ↗ 00:00

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 small, so this problem is not about asymptotic complexity. It tests a correct, 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 separate step, decide how to space them. Mixing the two steps causes most bugs.
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.

Write your solution, then hit Run tests to check it — or get a mock grade from the AI coach.

The coach remembers this session — revise your code and ask again, and it grades your progress. It gives hints, not the answer.
Report a bug