-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy path68.py
More file actions
15 lines (15 loc) · 724 Bytes
/
68.py
File metadata and controls
15 lines (15 loc) · 724 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution:
def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
res, used, s = [], 0, []
for i, w in enumerate(words):
if not s or len(w) + used + len(s) <= maxWidth:
used += len(w)
s += [w]
else:
if len(s) == 1:
res.append(s[0] + (maxWidth - used) * ' ')
else:
br = (maxWidth - used) // (len(s) - 1)
res.append(''.join((br + (i <= (maxWidth - used) % (len(s) - 1))) * ' ' + c for i, c in enumerate(s)).lstrip())
used, s = len(w), [w]
return res + [' '.join(c for c in s) + (maxWidth - used - len(s) + 1) * ' ']