-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathtext-justification.cc
More file actions
35 lines (33 loc) · 885 Bytes
/
text-justification.cc
File metadata and controls
35 lines (33 loc) · 885 Bytes
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
// Text Justification
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
vector<string> r;
for (int i = 0, j = 0; i < words.size(); i = j) {
int s = 0;
for (; j < words.size() && s+!!s+words[j].size() <= L; j++)
s += !!s+words[j].size();
if (j == words.size() || j-i == 1) {
string l;
FOR(k, i, j) {
if (! l.empty()) l += ' ';
l += words[k];
}
l.resize(L, ' ');
r.push_back(l);
} else {
int pad = (L-s)/(j-i-1), rest = (L-s)%(j-i-1);
string l;
FOR(k, i, j) {
if (! l.empty()) l += ' ';
l += words[k];
if (k+1 < j)
l += string(pad+(rest ? rest--, 1 : 0), ' ');
}
r.push_back(l);
}
}
return r;
}
};