class Solution: def fullJustify(self, words, maxWidth): result = [] line, line_length = [], 0 for word in words: if line_length + len(word) + len(line) <= maxWidth: line.append(word) line_length += len(word) else: spaces_needed = maxWidth - line_length if len(line) == 1: result.append(line[0] + ' ' * spaces_needed) else: space_between_words, extra_spaces = divmod(spaces_needed, len(line) - 1) line_str = '' for i in range(len(line) - 1): line_str += line[i] + ' ' * (space_between_words + (1 if i < extra_spaces else 0)) line_str += line[-1] result.append(line_str) line, line_length = [word], len(word) result.append(' '.join(line).ljust(maxWidth)) return result # Example usage if __name__ == "__main__": solution = Solution() words1 = ["This", "is", "an", "example", "of", "text", "justification."] words2 = ["What", "must", "be", "acknowledgment", "shall", "be"] words3 = ["Science", "is", "what", "we", "understand", "well", "enough", "to", "explain", "to", "a", "computer.", "Art", "is", "everything", "else", "we", "do"] maxWidth = 16 result1 = solution.fullJustify(words1, maxWidth) result2 = solution.fullJustify(words2, maxWidth) result3 = solution.fullJustify(words3, maxWidth) for line in result1: print(line) for line in result2: print(line) for line in result3: print(line)