KatInterview

Even-Width Paragraph Formatter

StringsAuto-graded · 22 test cases
MediumAuto-gradedStringsstringsgreedysimulation

Last updated

Greedily pack words into lines of a fixed width, then pad each line with spaces so it is exactly that wide, handling single-word and final lines separately.

Even-Width Paragraph Formatter

You are given a list of words and a target line width width. Format the words into lines so that every line is exactly width characters long.

Step 1: Packing words into lines

Process the words in their given order, without reordering or splitting them. Fill each line greedily: keep adding the next word to the current line as long as the line still fits when the words on it are separated by exactly one space. In other words, a line holding k words with total letter count L is allowed only if L + (k - 1) <= width. As soon as the next word would not fit, close the current line and begin a new line with that word.

Step 2: Padding each line to exactly width

Once the words of a line are chosen, build the line text as follows.

  • Regular line (it holds two or more words and is not the final line): spread the spaces so the line is exactly width characters long. The number of spaces to place in total is width minus the combined length of the words. They go into the gaps between neighbouring words. Every gap gets the same number of spaces, except that if the spaces do not divide evenly, the leftmost gaps each receive one extra space. Any two gaps therefore differ in size by at most one, and gaps to the left are never smaller than gaps to the right.
  • Single-word line (any line that contains only one word) and the final line of the output: place the words separated by one space each, and append spaces on the right until the length is width.

Return the list of formatted lines in order. Every string in the result must have length exactly width, and trailing spaces are significant.

Input

  • words: a list of non-empty strings, none containing a space.
  • width: a positive integer.

Output

A list of strings, each of length exactly width.

Constraints

  • 1 <= len(words)
  • 1 <= len(word) <= width for every word, so every word fits on a line by itself
  • Words contain no space characters
  • width is a positive integer

Examples

Example 1

Input: justify_text(['This', 'is', 'an', 'example', 'of', 'text', 'justification'], 16)

Output: ['This is an', 'example of text', 'justification ']

Example 2

Input: justify_text(['This', 'is', 'an', 'example', 'of', 'text', 'display'], 16)

Output: ['This is an', 'example of text', 'display ']

Hints

Hint 1

Separate the work into two phases for each line: first decide which words belong together, then decide how to lay out the spaces.

Hint 2

While extending a line starting at index `start`, adding `words[end]` needs `end - start` single spaces. Track the sum of word lengths separately so the fit test is `letters + len(words[end]) + (end - start) <= width`.

Hint 3

For a regular line, use `divmod(total_spaces, gaps)` to get the base gap size and the number of leftmost gaps that get one extra space. Handle single-word lines and the last line first, which also avoids dividing by zero.