-
Notifications
You must be signed in to change notification settings - Fork 11
/
Day-113.cpp
42 lines (39 loc) · 1.06 KB
/
Day-113.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
class Solution {
public:
string reverseWords(string s) {
reverse(s.begin() , s.end());
for(int i=0,j=0;i<(int)s.size();++i){
if(s[i]==' '){
if(i == 0){
continue;
}
reverse(s.begin()+j, s.begin()+i);
j = i+1;
}
if(i+1==s.size()){
reverse(s.begin()+j , s.begin()+i+1);
}
}
// if there were no redundant space then we don't require below operations and we can
// return 's'. But as redundant spaces are given in leetcode problem, I guess we can't
// do better.
string res;
int i = 0;
int j = (int)s.size()-1;
while(i<(int)s.size() && s[i]==' '){
++i;
}
while(j>i && s[j]==' '){
--j;
}
while(i<=j){
if(s[i]==' ' && s[i-1]==' '){
++i;
continue;
}
res+=s[i];
++i;
}
return res;
}
};