-
Notifications
You must be signed in to change notification settings - Fork 2
/
068 Text Justification.py
132 lines (113 loc) · 4.61 KB
/
068 Text Justification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
'''
68. Text Justification
https://leetcode.com/problems/text-justification/
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.
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.
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.
For the last line of text, it should be left-justified, and no extra space is inserted between words.
Note:
A word is defined as a character sequence consisting of non-space characters only.
Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
The input array words contains at least one word.
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: 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 because it contains only one word.
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:
1 <= words.length <= 300
1 <= words[i].length <= 20
words[i] consists of only English letters and symbols.
1 <= maxWidth <= 100
words[i].length <= maxWidth
'''
from typing import List
class Solution:
def fullJustify(self, allWords: List[str], lineWidth: int) -> List[str]:
wi = 0 # iterator on all words
def getWordsToPack():
'''
Get the words that can fit the line
'''
nonlocal wi
words = []
wordsWidth = 0 # sofar words width without spaces
while wi < len(allWords):
totalWidth = wordsWidth + len(allWords[wi]) + len(words) # sofar words width + new word width + between spaces
if totalWidth > lineWidth:
break
words.append(allWords[wi])
wordsWidth += len(allWords[wi])
wi += 1
return words, wordsWidth
def pack(words: List[str], wordsWidth: int) -> str:
'''
Pack the words
@words: words to pack
@wordsWith: total words width, without spaces
'''
spaceCount = lineWidth - wordsWidth
# Case 1: only 1 word
if len(words) == 1:
return words[0] + ' ' * spaceCount
gapCount = len(words) - 1
reminder = spaceCount % gapCount
if reminder == 0:
# Case 2: distribute space between words evenly
spaceWidth = spaceCount // gapCount
return (' ' * spaceWidth).join(words)
else:
# Case 3: there will be 2 kinds of spaces with 1 space difference
smallerGapWidth = spaceCount // gapCount
biggerGapWidth = smallerGapWidth + 1
biggerGapCount = reminder
packed = [words[0]]
for i in range(1, len(words)):
# Pack bigger gaps first
if i <= biggerGapCount:
packed.append(' ' * biggerGapWidth)
else:
packed.append(' ' * smallerGapWidth)
packed.append(words[i])
return ''.join(packed)
def packLastLine(words: List[str]) -> str:
wordsAndSpaces = ' '.join(words)
return wordsAndSpaces + ' ' * (lineWidth - len(wordsAndSpaces))
# Main
result = []
while wi < len(allWords):
words, wordsWidth = getWordsToPack()
if wi == len(allWords):
result.append(packLastLine(words))
else:
result.append(pack(words, wordsWidth))
return result