PIXELBANKv9.1.0
Menu

Given words and a maxWidth, format text with full justification: each line has exactly maxWidth characters, spaces distributed evenly. Last line is left-justified.

Input: first line = comma-separated words, second = maxWidth.

Example:

Input:
This,is,an,example,of,text,justification.
16
Output:
This    is    an
example  of text
justification.  
Reasoning:
  • The input words are split into an array: ["This", "is", "an", "example", "of", "text", "justification"].
  • We iterate through the words, distributing them into lines with a maximum width of 1616 characters, including spaces. The first line can fit ["This", "is", "an"], requiring 33 spaces to be added for even distribution: 5+2+2+3=125+2+2+3=12, and 44 more spaces are added to reach the maximum width.
  • The second line can fit ["example", "of", "text"], requiring 22 spaces between each word and 11 space at the end to reach a width of 1515, then one more space is added to reach the maximum width of 1616.
  • The last line is left-justified and contains the remaining word ["justification"], which is shorter than the maximum width, so it is printed as is.

Constraints:

  • 1 <= len(words) <= 300
  • 1 <= len(words[i]) <= 20
  • 1 <= maxWidth <= 100
solution.py

Test Results

0/0
Run code to see test results.