-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path27.TextJustificationAlgorithm.js
More file actions
46 lines (38 loc) · 1.58 KB
/
27.TextJustificationAlgorithm.js
File metadata and controls
46 lines (38 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// 27. Text Justification Algorithm
// Problem: Implement a text justification algorithm.
function fullJustify(words, maxWidth) {
let result = [];
let currentLine = [];
let currentLength = 0;
for (let word of words) {
if (currentLength + word.length + currentLine.length > maxWidth) {
let spaces = maxWidth - currentLength;
if (currentLine.length === 1) {
result.push(currentLine[0] + " ".repeat(spaces));
} else {
let spaceBetween = Math.floor(spaces / (currentLine.length - 1));
let extraSpaces = spaces % (currentLine.length - 1);
for (let i = 0; i < currentLine.length - 1; i++) {
currentLine[i] += " ".repeat(spaceBetween);
if (i < extraSpaces) {
currentLine[i] += " ";
}
}
result.push(currentLine.join(''));
}
currentLine = [];
currentLength = 0;
}
currentLine.push(word);
currentLength += word.length;
}
result.push(currentLine.join(" ") + " ".repeat(maxWidth - currentLength - (currentLine.length - 1)));
return result;
}
// Test case
const words = ["This", "is", "an", "example", "of", "text", "justification."];
const maxWidth = 16;
console.log(fullJustify(words, maxWidth));
// Explanation:
// -Justifies text by adjusting spaces between words to ensure each line is exactly maxWidth characters.
// -For the last line, words are left-aligned and the remaining space is distributed at the end.