Given a string which has a lot of spaces beween words . Remove these extra spaces in the string. eg: " I love New York " --> "I love New York"
string remove_extra_spaces(string s) {
if(s.empty()) return "";
int start = 0, N = s.size();
while(start < N && s[start] == ' ')
start++;
if(start == N) return "";
int end = N-1;
while(end >= 0 && s[end] == ' ') end--;
int curr = start, begin = start;
while(curr <= end) {
if(s[curr] != ' ')
s[begin++] = s[curr++];
else {
s[begin++] = s[curr++];
while(curr <= end && s[curr] == ' ') curr++;
}
}
return s.substr(start, begin-start);
}