Tutorialspoint
Problem
Solution
Submissions

Text Justification

Certification: Advanced Level Accuracy: 100% Submissions: 1 Points: 15

Write a Java program to format a given array of words such that each line has exactly a specified maximum width. The formatting rules are:

  • 1. Each line should contain as many words as possible.
  • 2. No word should be split across lines.
  • 3. Words should be separated by at least one space.
  • 4. For the last line, words should be left-justified with no extra spaces inserted between words.
  • 5. For all other lines, extra spaces should be distributed as evenly as possible between words, with the extra spaces, if any, distributed starting from the left.
Example 1
  • Input: words = ["This", "is", "an", "example", "of", "text", "justification."] maxWidth = 16
  • Output: [ "This is an", "example of text", "justification. " ]
  • Explanation: First line can fit "This", "is", and "an" (2 + 2 + 2 = 6 characters) with spaces in between. Total spaces needed for the first line: 16 - 6 = 10 spaces. Distribute spaces evenly: 5 spaces between "This" and "is", and 5 spaces between "is" and "an". For the second line, fit "example", "of", and "text" (7 + 2 + 4 = 13 characters). Total spaces needed: 16 - 13 = 3 spaces, distributed as 2 spaces between "example" and "of", and 1 space between "of" and "text". The last line has just "justification." and is left-justified with spaces added at the end.
Example 2
  • Input: words = ["What","must","be","acknowledgment","shall","be"] maxWidth = 16
  • Output: [ "What must be", "acknowledgment ", "shall be " ]
  • Explanation: First line fits "What", "must", and "be" (4 + 4 + 2 = 10 characters). Total spaces for first line: 16 - 10 = 6 spaces, distributed as 3 spaces between each word. Second line has only "acknowledgment" (13 characters) and needs 3 extra spaces at the end. The last line has "shall" and "be" (5 + 2 = 7 characters) with 1 space between them. Since it's the last line, it's left-justified with 8 extra spaces at the end.
Constraints
  • 1 <= words.length <= 300
  • 1 <= words[i].length <= 20
  • words[i] consists of only English letters and symbols
  • 1 <= maxWidth <= 100
  • words[i].length <= maxWidth
  • Time Complexity: O(n) where n is the total number of characters in all words
  • Space Complexity: O(n)
ArraysStringsPwCTutorix
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Process the words one by one, keeping track of the length of the current line
  • When adding a new word would exceed maxWidth, justify the current line and start a new one
  • For each line except the last one, calculate the number of spaces needed between words
  • Distribute the spaces evenly, with any extra spaces allocated from left to right
  • For the last line, left-justify the words with one space between each word
  • Pad the last line with extra spaces at the end to reach the maxWidth
  • Return the array of justified lines

Steps to solve by this approach:

 Step 1: Process words sequentially and determine how many words fit on each line without exceeding maxWidth.

 Step 2: For each line, calculate the total length of words and determine how many spaces are needed.
 Step 3: For lines that are not the last line, calculate how to distribute spaces evenly between words.
 Step 4: If spaces can't be distributed exactly evenly, add the extra spaces from left to right.
 Step 5: For the last line or lines with only one word, left-justify the text and pad with spaces at the end.
 Step 6: Build each line according to the justification rules and add them to the result list.
 Step 7: Return the list of justified lines.

Submitted Code :