68.Text Justification

Tags: [string]

Com: {fb}

Link: https://leetcode.com/problems/text-justification/\#/description

Given an array of words and a lengthL, format the text such that each line has exactlyLcharacters 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 exactlyLcharacters.

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 exceedLin 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.

Solution:

class Solution(object):
    def fullJustify(self, words, maxWidth):
        """
        :type words: List[str]
        :type maxWidth: int
        :rtype: List[str]
        """
        if not maxWidth:
            return ['']
        if not words:
            return ['' * maxWidth]

        result = []
        start = 0
        end = 0
        char_counter = 0
        word_counter = 0
        while end < len(words):
            curr_w = words[end]
            word_counter += 1
            char_counter += len(curr_w)

            if char_counter + (word_counter - 1) > maxWidth:
                word_counter -= 1
                char_counter -= len(curr_w)
                end -= 1

                num_of_slots = word_counter - 1
                num_of_space_each_slot = 0
                num_of_extra_space = 0
                num_of_padding_space = 0
                if num_of_slots > 0:
                    num_of_space_each_slot = (maxWidth - (char_counter + (word_counter - 1))) / num_of_slots
                    num_of_extra_space = (maxWidth - (char_counter + (word_counter - 1))) % num_of_slots
                else:
                    num_of_padding_space = maxWidth - char_counter

                buff = []
                for i in xrange(start, end + 1):
                    buff.append(words[i])
                    if i != end:
                        buff.append(' ')

                        for _ in xrange(num_of_space_each_slot):
                            buff.append(' ')

                        if num_of_extra_space > 0:
                            buff.append(' ')
                            num_of_extra_space -= 1

                for _ in xrange(num_of_padding_space):
                    buff.append(' ')

                result.append(''.join(buff))
                char_counter = 0
                word_counter = 0
                start = end + 1

            end += 1

        if end - start > 0:
            end -= 1
            num_of_padding_spaces = maxWidth - (char_counter + (word_counter - 1))
            buff = []
            for i in xrange(start, end + 1):
                buff.append(words[i])
                if i != end:
                    buff.append(' ')

            for _ in xrange(num_of_padding_spaces):
                buff.append(' ')

            result.append(''.join(buff))

        return result

Revelation:

  • Do not forget the 'num_of_padding_space'.

Note:

  • Time complexity = O(num of all chars).
  • Space complexity = O(maxWidth), not including the output space.

results matching ""

    No results matching ""