68. Text Justification

problem

solution

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
class Solution {
public:
string process(vector<string>& cur, int len, int cur_len){
string ret;
int n = cur.size();
if(n==1){
ret+=cur[0];
ret.append(len-cur_len,' ');
return ret;
}
int blank_size = (len - cur_len)/(n-1);
int mod = (len - cur_len)%(n-1);

for(int i=0;i<cur.size();++i){
if(i==cur.size()-1){
ret+=cur[i];
}
else{
ret+=cur[i];
ret.append(blank_size,' ');
if(mod>0) {
ret.append(1,' ');
mod--;
}

}
}
return ret;
}
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string> ret;
vector<string> cur;
int cur_len = 0;
for(int i=0;i<words.size();++i){
string word = words[i];
if(cur_len + cur.size()+word.size()<=maxWidth){
cur.push_back(word);
cur_len += word.size();
}
else{
string temp = process(cur, maxWidth, cur_len);
ret.push_back(temp);
cur_len = word.size();
cur.clear();
cur.push_back(word);
}
}

if(!cur.empty()){
string temp;
for(int i=0;i<cur.size();++i){
temp+=cur[i];
if(i!=cur.size()-1) temp.append(1,' ');
}
if(temp.size() < maxWidth) temp.append(maxWidth-temp.size(),' ');

ret.push_back(temp);
}
return ret;
}
};