Skip to content

Commit e2a8908

Browse files
authored
Update 68-text-justification.js
1 parent ffcf7c2 commit e2a8908

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

68-text-justification.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,40 @@
1+
/**
2+
* @param {string[]} words
3+
* @param {number} maxWidth
4+
* @return {string[]}
5+
*/
6+
const fullJustify = function(words, maxWidth) {
7+
const res = []
8+
let curRow = []
9+
let numOfChars = 0
10+
11+
for (let w of words) {
12+
if (numOfChars + w.length + curRow.length > maxWidth) {
13+
for(let i = 0; i < maxWidth - numOfChars; i++) {
14+
if(curRow.length === 1) {
15+
curRow[0] += ' '
16+
} else {
17+
curRow[i % (curRow.length - 1)] += ' '
18+
}
19+
}
20+
res.push(curRow.join(''))
21+
curRow = []
22+
numOfChars = 0
23+
}
24+
curRow.push(w)
25+
numOfChars += w.length
26+
}
27+
28+
const numOfSpace = maxWidth - numOfChars - (curRow.length - 1)
29+
let tail = ''
30+
for(let i = 0; i < numOfSpace; i++) tail += ' '
31+
res.push(curRow.join(' ') + tail)
32+
33+
return res
34+
};
35+
36+
// another
37+
138
/**
239
* @param {string[]} words
340
* @param {number} L

0 commit comments

Comments
 (0)