Notes

Personal notes on various topics

View on GitHub

Text Justification

Problem Statement

Given an array of strings words and an integer maxWidth, format the text so that each line has exactly maxWidth characters and is fully justified (both left and right).

Notes:

Examples

Example 1

Input:
words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16

Output:

[
    "This    is    an",
    "example  of text",
    "justification.  "
]

Example 2

Input:
words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16

Output:

[
  "What   must   be",
  "acknowledgment  ",
  "shall be        "
]

Explanation: The last line is left-justified. Lines with a single word are also left-justified.

Example 3

Input:
words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"], maxWidth = 20

Output:

[
  "Science  is  what we",
  "understand      well",
  "enough to explain to",
  "a  computer.  Art is",
  "everything  else  we",
  "do                  "
]

Constraints

Code Template

class Solution:
    def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
        # Your code here
        pass

Solutions

Back to Problem List Back to Categories