python solution


  • 0
    J
    class Solution(object):
        def fullJustify(self, words, maxWidth):
            result, low = [], words.pop(0)
            extra = maxWidth - len(low)
            for w in words:
                if extra >= len(w) + 1:
                    low += " " + w
                    extra -= len(w) + 1
                else:
                    count = low.count(" ")
                    if count == 0:
                        low += " " * extra
                    else:
                        q, m = extra / count + 1, extra % count
                        r = " " * q
                        low = low.replace(" ", r).replace(r, r+" ", m)
                    result.append(low)
                    low = w
                    extra = maxWidth - len(low)
            low += " " * extra
            result.append(low)
            return result
    

Log in to reply
 

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