-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_justification.cpp
More file actions
144 lines (120 loc) · 4.29 KB
/
text_justification.cpp
File metadata and controls
144 lines (120 loc) · 4.29 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
/*
68. Text Justification
Time Complexity: O(n) where n is total number of characters
Space Complexity: O(n) for the result
*/
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string> result;
int i = 0;
while (i < words.size()) {
vector<string> lineWords;
int lineLength = 0;
while (i < words.size()) {
int neededLength = words[i].length() + (lineWords.empty() ? 0 : 1) + lineLength;
if (neededLength <= maxWidth) {
lineWords.push_back(words[i]);
lineLength += words[i].length() + (lineWords.size() > 1 ? 1 : 0);
i++;
} else {
break;
}
}
string line;
if (i == words.size()) {
for (int j = 0; j < lineWords.size(); j++) {
line += lineWords[j];
if (j < lineWords.size() - 1) {
line += ' ';
}
}
line += string(maxWidth - line.length(), ' ');
result.push_back(line);
} else if (lineWords.size() == 1) {
line = lineWords[0] + string(maxWidth - lineWords[0].length(), ' ');
result.push_back(line);
} else {
int totalChars = 0;
for (const string& word : lineWords) {
totalChars += word.length();
}
int totalSpaces = maxWidth - totalChars;
int gaps = lineWords.size() - 1;
int spacesPerGap = totalSpaces / gaps;
int extraSpaces = totalSpaces % gaps;
for (int j = 0; j < lineWords.size(); j++) {
line += lineWords[j];
if (j < gaps) {
line += string(spacesPerGap, ' ');
if (j < extraSpaces) {
line += ' ';
}
}
}
result.push_back(line);
}
}
return result;
}
};
// Test cases
int main() {
Solution solution;
// Example 1
vector<string> words1 = {"This", "is", "an", "example", "of", "text", "justification."};
int maxWidth1 = 16;
vector<string> result1 = solution.fullJustify(words1, maxWidth1);
cout << "Example 1:" << endl;
for (const string& line : result1) {
cout << "\"" << line << "\"" << endl;
}
// Expected:
// "This is an"
// "example of text"
// "justification. "
cout << endl;
// Example 2
vector<string> words2 = {"What", "must", "be", "acknowledgment", "shall", "be"};
int maxWidth2 = 16;
vector<string> result2 = solution.fullJustify(words2, maxWidth2);
cout << "Example 2:" << endl;
for (const string& line : result2) {
cout << "\"" << line << "\"" << endl;
}
// Expected:
// "What must be"
// "acknowledgment "
// "shall be "
cout << endl;
// Example 3
vector<string> words3 = {"Science", "is", "what", "we", "understand", "well", "enough", "to",
"explain", "to", "a", "computer.", "Art", "is", "everything", "else", "we", "do"};
int maxWidth3 = 20;
vector<string> result3 = solution.fullJustify(words3, maxWidth3);
cout << "Example 3:" << endl;
for (const string& line : result3) {
cout << "\"" << line << "\"" << endl;
}
// Expected:
// "Science is what we"
// "understand well"
// "enough to explain to"
// "a computer. Art is"
// "everything else we"
// "do "
cout << endl;
// Edge case: single word per line
vector<string> words4 = {"a", "b", "c", "d", "e"};
int maxWidth4 = 3;
vector<string> result4 = solution.fullJustify(words4, maxWidth4);
cout << "Example 4:" << endl;
for (const string& line : result4) {
cout << "\"" << line << "\"" << endl;
}
return 0;
}