
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)
Editorial
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. |
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