Reverse Words 🚧

Problem

Given an input string, reverse the string word by word.

Example:

Input: “the sky is blue”,
Output: “blue is sky the”.

Note:

A word is defined as a sequence of non-space characters.
Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
You need to reduce multiple spaces between two words to a single space in the reversed string.

Follow up: For C programmers, try to solve it in-place in O(1) space.

Analysis

For an in-place O(1) space solution we can solve in three steps.

  1. Trim extra spaces
  2. Reverse whole string
  3. reverse each word

However, this is takes longer. If we have string with m words of average length x and is n characters long, it would take O(n+m*x)? 🚧

First Attempt

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
class Solution {
private:
void trim(string& s, int i) {
while (i < s.size()) {
if (s[i] == ' ') s.erase(i, 1);
else break;
}
}

string::iterator rword(string &s, string::iterator start) {
auto end = start;
while (*end != ' ') {
end++;
}
reverse(start, end);
return end;
}

public:
void reverseWords(string &s) {
// trim leading spaces
trim(s, 0);

// trim remaining extra spaces
int i = 0;
while (i < s.size()) {
if (s[i] != ' ') {
i++;
} else {
trim(s, i+1);
}

}

//reverse the whole input
reverse(s.begin(), s.end());

//reverse each word
auto it = s.begin();
while (it != s.end()) {
it = rword(s, it);
it++; //skip spaces between words
}
}
};

This one goes into an infinite loop.

Accepted (slow)

Used Visual Studio Debugger to step through and find the cycle. Looks like we need to be careful about moving iterators while maintaining single space between words. All in all this is a very complex 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
62
63
64
65
66
67
68
69
// ReverseWords.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <algorithm>
#include <iostream>

using namespace std;

class Solution {
private:
int trim(string& s, int i) {
while (i < (int) s.size()) {
if (s[i] == ' ') s.erase(i, 1);
else break;
}
return i + 1; // skip to non-space char (may be past end of string)
}

string::iterator rword(string &s, string::iterator start) {
auto end = start;
while (end != s.end() && *end != ' ') {
end++;
}
reverse(start, end);
return end;
}

public:
void reverseWords(string &s) {
// trim leading spaces
trim(s, 0);

// trim remaining extra spaces
int i = 0;
while (i < (int) s.size()) {
if (s[i] != ' ') {
i++;
}
else {
i = trim(s, i + 1);
}

}

//reverse the whole input
reverse(s.begin(), s.end());

// trim any spaces left over from last run
trim(s, 0);

//reverse each word
auto it = s.begin();
while (it != s.end()) {
it = rword(s, it);
if (it != s.end()) it++; //skip spaces between words
}
}
};

int main()
{
Solution sol;
string str = " This is a test ";
sol.reverseWords(str);
cout << "Reversed: " << str << endl;
return 0;
}

Accepted(improved) [TODO]

With C++ stringstream is an effective option for problems such as these.

1
2