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
maxWidth
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:
# Explanation
  1. Initialization : Start by initializing some variables to keep track of the current line, the current length of the line, and the final result.
  2. Building Lines : Iterate through the words list and keep adding words to the current line until adding another word would exceed
    maxWidth
    .
  3. Distributing Spaces : When adding another word would exceed
    maxWidth
    , distribute spaces in the current line to fully justify it:
    • 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.
  4. Left-Justification for the Last Line : After processing all words, left-justify the last line (since the last line should not be fully justified).
  5. Append to Result : Append each fully justified (or left-justified for the last line) to the final result.
  6. # Detailed Steps in Pseudocode
  7. Initialize Needed Variables :
    • result_list
      : To store the final list of justified lines.
    • current_line
      : To store words of the current line.
    • current_line_length
      : To keep track of the length of the current line.
  8. 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.
  9. Justify the Current Line :
    • When a new word exceeds the
      maxWidth
      , you distribute spaces:
      • 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.
  10. 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
      is reached.
  11. Return the Result :
    • Finally, return the
      result_list
      .
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
characters, ensuring even distribution of spaces between words while handling edge cases like uneven space spread and left-justified last line.