-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path0068-text-justification.py
52 lines (36 loc) · 1.37 KB
/
0068-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
from typing import List
class Solution:
def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
def getWords(i):
currentLine = []
currLength = 0
while i < len(words) and currLength + len(words[i]) <= maxWidth:
currentLine.append(words[i])
currLength += len(words[i]) + 1
i += 1
return currentLine
def createLine(line, i):
baseLength = -1
for word in line:
baseLength += len(word) + 1
extraSpaces = maxWidth - baseLength
if len(line) == 1 or i == len(words):
return " ".join(line) + " " * extraSpaces
wordCount = len(line) - 1
spacesPerWord = extraSpaces // wordCount
needsExtraSpace = extraSpaces % wordCount
for j in range(needsExtraSpace):
line[j] += " "
for j in range(wordCount):
line[j] += " " * spacesPerWord
return " ".join(line)
ans = []
i = 0
while i < len(words):
currentLine = getWords(i)
i += len(currentLine)
ans.append(createLine(currentLine, i))
return ans
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
print(Solution().fullJustify(words, maxWidth))