|
| 1 | +class Solution(object): |
| 2 | + def fullJustify(self, words, maxWidth): |
| 3 | + """ |
| 4 | + :type words: List[str] |
| 5 | + :type maxWidth: int |
| 6 | + :rtype: List[str] |
| 7 | + """ |
| 8 | + currentWordIdx = 0 # the index of the current word |
| 9 | + ans = [] |
| 10 | + while currentWordIdx < len(words): |
| 11 | + wordCountForEachLine = self.getWordsCountForEachLine(currentWordIdx, words, maxWidth) |
| 12 | + line = self.insertSpaces(currentWordIdx, wordCountForEachLine, words, maxWidth) # create a line which contains words from words[currentWordIdx] to words[currentWordIdx + wordCountForEachLine - 1] |
| 13 | + ans.append(line) |
| 14 | + currentWordIdx += wordCountForEachLine |
| 15 | + return ans |
| 16 | + |
| 17 | + |
| 18 | + # How many words we need to form each line; |
| 19 | + def getWordsCountForEachLine(self, currentWordIdx, words, maxWidth): |
| 20 | + wordCountForLine = 1 # figure out how many words can fit into a line, which starts from 1 ssince we are adding one word initially |
| 21 | + currentLen = len(words[currentWordIdx]) |
| 22 | + while currentWordIdx + wordCountForLine < len(words): |
| 23 | + nextLen = len(words[currentWordIdx + wordCountForLine]) + 1 # here, the last added 1 is for an added space at the left |
| 24 | + if currentLen + nextLen <= maxWidth: |
| 25 | + wordCountForLine += 1 |
| 26 | + currentLen += nextLen |
| 27 | + else: |
| 28 | + break |
| 29 | + return wordCountForLine |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + # How many spaces we should insert between two words. |
| 34 | + def insertSpaces(self, currentWordIdx, wordCountForEachLine, words, maxWidth): |
| 35 | + initialLine = ' '.join(words[currentWordIdx:currentWordIdx + wordCountForEachLine]) # Concatenate words[currentWordIdx:currentWordIdx + wordCountForEachLine] into one line |
| 36 | + finalLine = "" |
| 37 | + if wordCountForEachLine == 1 or currentWordIdx + wordCountForEachLine == len(words): # if the line contains only one word or it is the last line |
| 38 | + spaces = maxWidth - len(initialLine) # we just need to left assigned it |
| 39 | + finalLine = initialLine + ' '*spaces |
| 40 | + else: |
| 41 | + totalSpaces = maxWidth - len(initialLine) + (wordCountForEachLine - 1) # total number of spaces we need insert |
| 42 | + averageSpaces = totalSpaces // (wordCountForEachLine - 1) # average number of spaces we should insert between two words |
| 43 | + leftWords = totalSpaces % (wordCountForEachLine - 1) # number of 'left' words, i.e. words that have 1 more space than the other words on the right side |
| 44 | + if leftWords > 0: |
| 45 | + finalLine = (" "*(averageSpaces + 1)).join(words[currentWordIdx:currentWordIdx + leftWords]) # left words. The reason for +1 is, these words will have one more spaces then others |
| 46 | + finalLine += " "*(averageSpaces + 1) # spaces between left words & right words |
| 47 | + finalLine += (" "*averageSpaces).join(words[currentWordIdx + leftWords:currentWordIdx + wordCountForEachLine]) # right words |
| 48 | + else: |
| 49 | + finalLine = (" "*averageSpaces).join(words[currentWordIdx:currentWordIdx + wordCountForEachLine]) |
| 50 | + return finalLine |
| 51 | + |
0 commit comments