Skip to content

68. Text Justification

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

js
/**
 * @param {string[]} words
 * @param {number} maxWidth
 * @return {string[]}
 */
var fullJustify = function(words, maxWidth) {
  var result = []
  var current = []
  var curLen = 0

  for (let i = 0; i < words.length; i++) {
    let word = words[i]

    if (curLen + word.length <= maxWidth) {
      current.push(word)
      curLen += word.length + 1
    } else {
      result.push(adjust(current, maxWidth))
      current = [word]
      curLen = word.length + 1
    }
  }

  var last = current.join(' ')
  var pads = maxWidth - last.length
  for (let i = 0; i < pads; i++) {
    last += ' '
  }
  result.push(last)

  return result

  function adjust(words, maxWidth) {
    var len = words.reduce((ret, word) => ret + word.length, 0)
    var pads = maxWidth - len

    if (words.length === 1) {
      for (let i = 0; i < pads; i++) {
        words.push(' ')
      }
      return words.join('')
    }

    var avg = pads / (words.length - 1) >> 0
    var mod = pads % (words.length - 1)
    var result = []

    for (let i = 0; i < words.length - 1; i++) {
      result.push(words[i])
      if (i < mod) {
        result.push(' ')
      }
      for (let j = 0; j < avg; j++) {
        result.push(' ')
      }
    }
    result.push(words[words.length - 1])

    return result.join('')
  }
}
py
from functools import reduce


class Solution(object):

    def fullJustify(self, words, maxWidth):
        """
        :type words: List[str]
        :type maxWidth: int
        :rtype: List[str]
        """
        result = []
        current = []
        curlen = 0

        for word in words:
            if curlen + len(word) <= maxWidth:
                current.append(word)
                curlen += len(word) + 1
            else:
                result.append(self.adjust(current, maxWidth))
                current = [word]
                curlen = len(word) + 1

        last = ' '.join(current)
        pads = maxWidth - len(last)
        for i in range(pads):
            last += ' '
        result.append(last)

        return result

    def adjust(self, words, maxWidth):
        width = reduce(lambda a, b: a + len(b), words, 0)
        pads = maxWidth - width

        if len(words) == 1:
            for i in range(pads):
                words.append(' ')
            return ''.join(words)

        avg, mod = divmod(pads, len(words) - 1)
        result = []

        for i in range(len(words)-1):
            result.append(words[i])
            if i < mod:
                result.append(' ')
            for j in range(avg):
                result.append(' ')
        result.append(words[-1])

        return ''.join(result)