Text Justification
Input:
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
Output:
[
"This is an",
"example of text",
"justification. "
]Last updated
Input:
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
Output:
[
"This is an",
"example of text",
"justification. "
]Last updated
Input:
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
Output:
[
"What must be",
"acknowledgment ",
"shall be "
]
Explanation: Note that the last line is "shall be " instead of "shall be",
because the last line must be left-justified instead of fully-justified.
Note that the second line is also left-justified becase it contains only one word.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 "
]
for j in range(maxWidth-letterLen):
cur[j%(len(cur)-1 or 1)] +=' ' #curclass Solution:
def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
cur,letterLen,res = [],0,[]
for w in words:
if letterLen + len(cur) + len(w) >maxWidth:
for j in range(maxWidth-letterLen):
cur[j%(len(cur)-1 or 1)] +=' '
res.append(''.join(cur))
cur, letterLen = [], 0
cur += [w]
letterLen += len(w)
return res +[' '.join(cur).ljust(maxWidth)]