Text Justification
To solve this coding challenge, you need to format a given list of words into a fully justified text where each line has exactly
characters. Each line (except for the last one) should have words separated by spaces as evenly as possible.
We'll approach this problem step by step, using a greedy algorithm to pack as many words as possible into each line and then distribute spaces accordingly. Here's how we can achieve it:
characters, ensuring even distribution of spaces between words while handling edge cases like uneven space spread and left-justified last line.
maxWidth
# Explanation
- Initialization : Start by initializing some variables to keep track of the current line, the current length of the line, and the final result.
-
Building Lines
: Iterate through the words list and keep adding words to the current line until adding another word would exceed
maxWidth
-
Distributing Spaces
: When adding another word would exceed
maxWidth
- Calculate the total number of spaces needed.
- Distribute the extra spaces evenly between the words.
- Handle cases where spaces don't divide evenly by adding extra spaces to the left slots.
- Left-Justification for the Last Line : After processing all words, left-justify the last line (since the last line should not be fully justified).
- Append to Result : Append each fully justified (or left-justified for the last line) to the final result.
- Initialize Needed Variables :
-
result_list
-
current_line
-
current_line_length
- Iterate Through Words :
-
For each word in
words
-
Check if adding the new word exceeds
maxWidth
- If it exceeds, format the current line and reset variables for a new line.
- If it does not exceed, add the word to the current line and update the current line length.
- Justify the Current Line :
-
When a new word exceeds the
maxWidth
- Calculate extra spaces required.
- Distribute spaces evenly.
- Handle extra spaces that donβt divide evenly by adding them to the leftmost slots.
- Move the current line to the result list after formatting.
- Left-Justify the Last Line :
-
For the last line, left-justify it by adding spaces only to the end of the current line until
maxWidth
- Return the Result :
-
Finally, return the
result_list
# Detailed Steps in Pseudocode
Pseudocode with Comments
# Initialize variables
initialize 'result_list' as an empty list
initialize 'current_line' as an empty list
initialize 'current_line_length' as 0
# Iterate through each word
for each word in 'words':
# Check if adding this word would exceed maxWidth
if 'current_line_length' + length of 'word' + number of words in 'current_line' > 'maxWidth':
# We need to distribute spaces to justify 'current_line'
# Calculate total spaces needed
spaces_needed = 'maxWidth' - 'current_line_length'
# Initialize space slots
number_of_gaps = length of 'current_line' - 1 or 1 # to avoid division by zero
# Distribute spaces
for i in range(spaces_needed):
'current_line[i % number_of_gaps]' += ' ' # distribute spaces evenly
# Join words in 'current_line' and add to result_list
result_line = ''.join('current_line')
'result_list'.append(result_line)
# Reset for the new line
'current_line' = [word]
'current_line_length' = length of 'word'
else:
# Add the word to the current line
'current_line'.append('word')
'current_line_length' += length of 'word'
# Process the last line (left-justified)
result_line = ' '.join('current_line').ljust('maxWidth')
'result_list'.append(result_line)
return 'result_list'
By following these detailed steps and pseudocode, you can fully justify the given text into lines of exactly
maxWidth