Back to DSA sheet

Text Justification

HardArrays

Given words and a maxWidth, format the text so each line is exactly maxWidth characters, fully justified (extra spaces distributed as evenly as possible, with more spaces on the left). The last line is left-justified with a single space between words and padded on the right.

Input format (stdin): line 1 has the word count m and maxWidth. The next m lines each hold one word. Output the justified lines.

Examples
Input: 7 16 This is an example of text justification.
Output: This is an example of text justification.
Each of the first lines is exactly 16 wide; the last is left-justified.
Constraints
  • 1 <= m <= 300
  • 1 <= word length <= maxWidth <= 100
Sheets
NeetCode 250
text-justification.cpp2 sample tests
Loading editor
Test results

Run the sample tests to check your solution against expected output.

Custom input (stdin)
Output

Run your code to see its output.