hoangreal

Text Justification

Oct 21st, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.83 KB | None | 0 0
  1. """
  2. Given an array of strings words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified.
  3.  
  4. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly maxWidth characters.
  5.  
  6. Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line does not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
  7.  
  8. For the last line of text, it should be left-justified, and no extra space is inserted between words.
  9.  
  10. Note:
  11.  
  12. A word is defined as a character sequence consisting of non-space characters only.
  13. Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
  14. The input array words contains at least one word.
  15. """
  16.  
  17. class Solution:
  18.     def fullJustify(self, words, maxWidth):
  19.         """
  20.        Given a list of words and a maximum width maxWidth, format the text
  21.        such that each line has exactly maxWidth characters and is
  22.        fully (left and right) justified. Words should be packed in a greedy approach.
  23.        """
  24.         justified_text = []  # Resultant list that will contain the fully justified text
  25.         current_index, total_words = 0, len(words)  # Initialize current index and get the total number of words
  26.        
  27.         # Iterate over words to construct each line
  28.         while current_index < total_words:
  29.             line_words = []  # Words to be included in the current line
  30.             count = len(words[current_index])  # Character count in the current line, starting with the first word
  31.             line_words.append(words[current_index])  # Add first word to the line
  32.             current_index += 1  # Move to the next word
  33.            
  34.             # Try to fit as many words as possible into the current line
  35.             while current_index < total_words and count + 1 + len(words[current_index]) <= maxWidth:
  36.                 count += 1 + len(words[current_index])  # Update count including spaces between words
  37.                 line_words.append(words[current_index])  # Add next word to the line
  38.                 current_index += 1  # Move to the next word
  39.            
  40.             # Check if it's the last line or contains only one word
  41.             if current_index == total_words or len(line_words) == 1:
  42.                 # Left-justify the line
  43.                 line_left_justified = ' '.join(line_words)
  44.                 # Calculate the remaining spaces on the right
  45.                 spaces_on_right = ' ' * (maxWidth - len(line_left_justified))
  46.                 # Add the left-justified and right-padded line to result
  47.                 justified_text.append(line_left_justified + spaces_on_right)
  48.                 continue
  49.            
  50.             # Not the last line or a single word, fully justify the line
  51.             total_spaces = maxWidth - (count - len(line_words) + 1)  # Calculate total spaces to fill
  52.             space_width, extra_spaces = divmod(total_spaces, len(line_words) - 1)  # Evenly distribute spaces
  53.            
  54.             constructed_line = []
  55.             # Distribute space_width (+1 for extra_spaces) among words
  56.             for index, word in enumerate(line_words[:-1]):
  57.                 constructed_line.append(word)
  58.                 # Append the appropriate number of spaces after the current word
  59.                 constructed_line.append(' ' * (space_width + (1 if index < extra_spaces else 0)))
  60.            
  61.             constructed_line.append(line_words[-1])  # Append the last word without extra spaces
  62.             justified_text.append(''.join(constructed_line))  # Add the fully justified line to result
  63.        
  64.         return justified_text  # Return the justified text as a list of lines
  65.  
Add Comment
Please, Sign In to add comment