2
* @param {string[]} words3
* @param {number} maxWidth6
var fullJustify = function (words, maxWidth) {7
if (!words || !words.length) return;12
words.forEach((word, i) => {13
if (count + word.length + wordCols.length > maxWidth) {14
wordRows.push(wordCols);22
if (i === words.length - 1) {23
wordRows.push(wordCols);27
return wordRows.map((rowWords, i) =>28
justifyText(rowWords, maxWidth, i === wordRows.length - 1)32
const justifyText = (rowWords, maxWidth, isLastLine) => {33
let spaces = maxWidth - rowWords.reduce((acc, curr) => acc + curr.length, 0);35
if (rowWords.length === 1) {36
return rowWords[0] + " ".repeat(spaces);40
spaces -= rowWords.length - 1;41
return rowWords.join(" ") + " ".repeat(spaces);44
let index = rowWords.length - 1;45
let justifiedWord = "";46
while (rowWords.length > 0) {47
const repeater = Math.floor(spaces / (rowWords.length - 1));48
const word = rowWords.pop();51
justifiedWord = word + justifiedWord;52
} else if (index === 1) {53
justifiedWord = " ".repeat(spaces) + word + justifiedWord;55
justifiedWord = " ".repeat(repeater) + word + justifiedWord;