-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext-justification.js
More file actions
188 lines (157 loc) · 4.56 KB
/
text-justification.js
File metadata and controls
188 lines (157 loc) · 4.56 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* Problem: Text Justification
* Link: https://leetcode.com/problems/text-justification/
* Difficulty: Hard
*
* Format text to fit maxWidth characters per line with full justification.
*
* Example:
* Input: words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16
* Output: [
* "This is an",
* "example of text",
* "justification. "
* ]
*
* Time Complexity: O(n) where n is total characters
* Space Complexity: O(m) where m is maxWidth
*/
// JavaScript Solution
function fullJustify(words, maxWidth) {
const result = [];
let i = 0;
while (i < words.length) {
// Find how many words fit in current line
let lineWords = [];
let lineLength = 0;
while (i < words.length) {
// Check if adding next word would exceed maxWidth
// (including at least 1 space between words)
const wordLength = words[i].length;
const spacesNeeded = lineWords.length; // minimum spaces between words
if (lineLength + wordLength + spacesNeeded > maxWidth) {
break;
}
lineWords.push(words[i]);
lineLength += wordLength;
i++;
}
// Build the line
let line;
// Last line or single word line - left justify
if (i === words.length || lineWords.length === 1) {
line = lineWords.join(" ");
line += " ".repeat(maxWidth - line.length);
} else {
// Middle lines - full justify
const totalSpaces = maxWidth - lineLength;
const gaps = lineWords.length - 1;
const spacesPerGap = Math.floor(totalSpaces / gaps);
const extraSpaces = totalSpaces % gaps;
line = "";
for (let j = 0; j < lineWords.length; j++) {
line += lineWords[j];
if (j < gaps) {
// Add base spaces
line += " ".repeat(spacesPerGap);
// Add extra space to leftmost gaps
if (j < extraSpaces) {
line += " ";
}
}
}
}
result.push(line);
}
return result;
}
// Test cases
console.log(
fullJustify(
["This", "is", "an", "example", "of", "text", "justification."],
16
)
);
// ["This is an", "example of text", "justification. "]
console.log(
fullJustify(["What", "must", "be", "acknowledgment", "shall", "be"], 16)
);
// ["What must be", "acknowledgment ", "shall be "]
console.log(
fullJustify(
[
"Science",
"is",
"what",
"we",
"understand",
"well",
"enough",
"to",
"explain",
"to",
"a",
"computer.",
"Art",
"is",
"everything",
"else",
"we",
"do",
],
20
)
);
module.exports = fullJustify;
/* Python Solution (commented):
def full_justify(words: list[str], max_width: int) -> list[str]:
"""
Format text with full justification
Args:
words: List of words to format
max_width: Maximum characters per line
Returns:
List of justified lines
Time Complexity: O(n)
Space Complexity: O(m)
"""
result = []
i = 0
while i < len(words):
# Find how many words fit in current line
line_words = []
line_length = 0
while i < len(words):
word_length = len(words[i])
spaces_needed = len(line_words)
if line_length + word_length + spaces_needed > max_width:
break
line_words.append(words[i])
line_length += word_length
i += 1
# Build the line
# Last line or single word line - left justify
if i == len(words) or len(line_words) == 1:
line = ' '.join(line_words)
line += ' ' * (max_width - len(line))
else:
# Middle lines - full justify
total_spaces = max_width - line_length
gaps = len(line_words) - 1
spaces_per_gap = total_spaces // gaps
extra_spaces = total_spaces % gaps
line = ''
for j in range(len(line_words)):
line += line_words[j]
if j < gaps:
# Add base spaces
line += ' ' * spaces_per_gap
# Add extra space to leftmost gaps
if j < extra_spaces:
line += ' '
result.append(line)
return result
# Test cases
print(full_justify(["This", "is", "an", "example", "of", "text", "justification."], 16))
print(full_justify(["What","must","be","acknowledgment","shall","be"], 16))
*/