# Python straight forward solution

• ``````class Solution(object):
def fullJustify(self, words, maxWidth):
if not words:
return []
result = []
i, j = 0, 0
currentLen = 0
while j < len(words):
if currentLen + len(words[j]) <= maxWidth:
currentLen += len(words[j]) + 1
j += 1
else:
if i == j-1:
result.append(words[i] + ' ' * (maxWidth - len(words[i])))
else:
number_of_interval = j-i-1
total_length = sum(map(len, words[i:j]))
total_space = maxWidth - total_length
# total_space%number_of_interval  of total_space/number_of_interval +1 spaces
# and the rest of total_space/number_of_interval spaces
retStr = (" "*((total_space/number_of_interval)+1)).join(words[i: i+1+total_space%number_of_interval])
retStr = (" "*((total_space/number_of_interval))).join([retStr] + words[i+1+total_space%number_of_interval:j])
result.append(retStr)
currentLen = 0
i = j
result.append(" ".join(words[i:j]).ljust(maxWidth))
return result
``````

Simply iter through the array and construct the string when length exceed maxwidth. Do a little math to get the number of spaces to inject. ljust the last line.

• A great help for me, a python greenhand. Thanks

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.