will format the string such that each line has exactly maxwidth characters and is fully left and right justified and return the list of justified text example 1 string this is an example of text justification maxwidth 16 output this is an example of text justification textjustificationthis is an example of text justification 16 this is an example of text justification example 2 string two roads diverged in a yellow wood maxwidth 16 output two roads diverged in a yellow wood textjustificationtwo roads diverged in a yellow wood 16 two roads diverged in a yellow wood time complexity omn space complexity omn converting string into list of strings split by a space if there is only word in line just insert overallspacescount for the remainder of line numspacesbetweenwordslisti tells you to insert numspacesbetweenwordslisti spaces after word on linei distribute spaces via round robin to the left words add the word add the spaces to insert just add the last word to the sentence join the aligned words list to form a justified line keep adding words until we can fill out maxwidth width sum of length of all words without overallspacescount lenword length of current word lenline number of overallspacescount to insert between words justify the line and add it to result reset new line and new width will format the string such that each line has exactly max_width characters and is fully left and right justified and return the list of justified text example 1 string this is an example of text justification max_width 16 output this is an example of text justification text_justification this is an example of text justification 16 this is an example of text justification example 2 string two roads diverged in a yellow wood max_width 16 output two roads diverged in a yellow wood text_justification two roads diverged in a yellow wood 16 two roads diverged in a yellow wood time complexity o m n space complexity o m n converting string into list of strings split by a space if there is only word in line just insert overall_spaces_count for the remainder of line num_spaces_between_words_list i tells you to insert num_spaces_between_words_list i spaces after word on line i distribute spaces via round robin to the left words add the word add the spaces to insert just add the last word to the sentence join the aligned words list to form a justified line keep adding words until we can fill out max_width width sum of length of all words without overall_spaces_count len word length of current word len line number of overall_spaces_count to insert between words justify the line and add it to result reset new line and new width
def text_justification(word: str, max_width: int) -> list:
    

    
    words = word.split()

    def justify(line: list, width: int, max_width: int) -> str:
        overall_spaces_count = max_width - width
        words_count = len(line)
        if len(line) == 1:
            
            
            return line[0] + " " * overall_spaces_count
        else:
            spaces_to_insert_between_words = words_count - 1
            
            
            
            num_spaces_between_words_list = spaces_to_insert_between_words * [
                overall_spaces_count // spaces_to_insert_between_words
            ]
            spaces_count_in_locations = (
                overall_spaces_count % spaces_to_insert_between_words
            )
            
            for i in range(spaces_count_in_locations):
                num_spaces_between_words_list[i] += 1
            aligned_words_list = []
            for i in range(spaces_to_insert_between_words):
                
                aligned_words_list.append(line[i])
                
                aligned_words_list.append(num_spaces_between_words_list[i] * " ")
            
            aligned_words_list.append(line[-1])
            
            return "".join(aligned_words_list)

    answer = []
    line: list[str] = []
    width = 0
    for word in words:
        if width + len(word) + len(line) <= max_width:
            
            
            
            
            line.append(word)
            width += len(word)
        else:
            
            answer.append(justify(line, width, max_width))
            
            line, width = [word], len(word)
    remaining_spaces = max_width - width - len(line)
    answer.append(" ".join(line) + (remaining_spaces + 1) * " ")
    return answer


if __name__ == "__main__":
    from doctest import testmod

    testmod()