Leetcode 68: Text Justification

https://leetcode.com/problems/text-justification/

Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.

You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly Lcharacters.

Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.

For the last line of text, it should be left justified and no extra space is inserted between words.

For example,
words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16.

Return the formatted lines as:

[
   "This    is    an",
   "example  of text",
   "justification.  "
]

Note: Each word is guaranteed not to exceed L in length.

click to show corner cases.

Corner Cases:

  • A line other than the last line might contain only one word. What should you do in this case?
    In this case, that line should be left-justified.

 

Code

class Solution(object):
    def fullJustify(self, words, maxWidth):
        """
        :type words: List[str]
        :type maxWidth: int
        :rtype: List[str]
        """
        res = []
        cur = 0                   # number of characters (without spaces) in current line 
        cur_l = []                # word list for current line
        i = 0
        
        while i < len(words):
            w =  words[i]
            if len(w) <= maxWidth - cur - len(cur_l):       # More words can be included in current line
                cur_l += [w]
                cur += len(w)
                i += 1
                if  i != len(words):                        # continue if the word is not the last word
                    continue
            
            if len(cur_l)==1:                               # This line only contains one word
                 res += [cur_l[0] + ' '*(maxWidth-cur)]
            else:
                if i == len(words):              # only left justified
                    line = ' '.join(cur_l)  
                    line += ' ' * (maxWidth - len(line))
                else:
                    line = cur_l[0]              # left-right justified
                    for ii, ww in enumerate(cur_l[1:], start=1):
                        if i != len(words):  
                            line += ' ' * ((maxWidth-cur)/(len(cur_l)-1)) 
                            if (maxWidth-cur) % (len(cur_l)-1) >= ii:
                                line += ' '
                            line += ww

                res += [line]
                
            if i != len(words):
                cur_l = []
                cur = 0
    
        return res

 

Idea

Lots of conditions to be considered. See comments for explanation. To me there seems not to be an intelligently challenging problem.

Leave a comment

Your email address will not be published. Required fields are marked *