/
Text Justification.cpp
71 lines (71 loc) · 2.05 KB
/
Text Justification.cpp
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
class Solution {
int find_end(vector<string> &words, int start, int L) {
int len=words[start].length();
int end = start+1;
while (end<words.size()) {
len ++;
len +=words[end].length();
if (len>L) {
break;
}
end++;
}
return end;
}
string justify(vector<string> &words, int start, int end, int L) {
int clen = 0;
for(int i=start; i<end; i++) {
clen = clen + words[i].length();
}
int slen = L - clen;
if (end-start == 1) {
string s = words[start];
for (int i=0; i<slen; i++) {
s+=' ';
}
return s;
}
else if(end!=words.size()) {
int average = slen/(end-start-1);
int more = slen - (end-start-1)*average;
string s = words[start];
for (int i=start+1;i<end;i++){
int loop = average;
if (i-start<more+1) {
loop = average+1;
}
for (int j=0; j<loop; j++) {
s+=' ';
}
s+=words[i];
}
return s;
}
else {
string s = words[start];
for (int i=start+1; i<end; i++) {
s+=' ';
s+=words[i];
}
int len = s.length();
for (int i=0; i<L-len; i++) {
s+=' ';
}
return s;
}
}
public:
vector<string> fullJustify(vector<string> &words, int L) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<string> ret;
int start = 0;
while (start<words.size()) {
int end = find_end(words, start, L);
string s = justify(words, start, end, L);
ret.push_back(s);
start = end;
}
return ret;
}
};