forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1592.cpp
More file actions
46 lines (46 loc) · 1.03 KB
/
1592.cpp
File metadata and controls
46 lines (46 loc) · 1.03 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
class Solution {
public:
string reorderSpaces(string text) {
string s="",word;
vector<string> words;
int spaces=0,i=0;
while(i<text.size()){
if(text[i]==' '){
spaces++;
i++;
}
else{
word="";
while(i<text.size() && text[i]!=' '){
word+=text[i];
i++;
}
words.push_back(word);
}
}
if(words.size()==1){
s+=words[0];
while(spaces){
s+=' ';
spaces--;
}
return s;
}
int n=spaces/(words.size()-1);
for(auto word: words){
s+=word;
int temp=n;
if(spaces<n) break;
while(temp){
s+=' ';
temp--;
}
spaces-=n;
}
while(spaces){
s+=' ';
spaces--;
}
return s;
}
};