-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreverseWords.cpp
More file actions
72 lines (68 loc) · 2.18 KB
/
reverseWords.cpp
File metadata and controls
72 lines (68 loc) · 2.18 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/****************************************************************
* Given an input string, reverse the string word by word.
* For example, given s = "the sky is blue",
* return "blue is sky the"
*
* Additional constraints:
* 1)reversed string should not contain leading or trailing spaces
* 2)multiple spaces between two words in input string to be
* reduced to a single space in the reversed string
*
* Implementation Logic:
* 1) Reverse all characters:
* "the sky is blue" ---> "eulb si yks eht" -- O(n) time
* 2) Reverse each word
* "eulb si yks eht" ---> "blue is sky the" -- O(n) time
****************************************************************/
#include <string>
using namespace std;
class Solution {
private:
/* Reverse a portion of the string from start pos to
end pos */
void reverse(string &s, int start, int end)
{
for (int i = start, j = end-1; i < j; i++, j--)
{
/* Swap */
char temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
/* Reverse every word in the string */
void reverseEachWord(string &s)
{
int startIdx = 0, endIdx = 0;
while (startIdx < s.size()) {
/* Words are seperated by white space */
endIdx = s.find(' ', startIdx);
if (endIdx == -1) { /* last word */
endIdx = s.size();
}
/* Reverse a word */
reverse(s, startIdx, endIdx);
startIdx = endIdx + 1;
/* Delete trailing white space */
while ((startIdx < s.size()) &&
(s[startIdx] == ' ')) {
s.erase(startIdx, 1);
}
}
/* Erase last space */
if (s[s.size()-1] == ' ') { s.erase(s.size()-1, 1); }
}
public:
void reverseWords(string &s) {
int i = s.size() - 1;
/* Erase trailing spaces characters */
while ((i > 0) && (s[i] == ' ')) {
s.erase(i, 1);
i--;
}
/* First the reverse the all characters */
reverse(s, 0, s.size());
/* Reverse each word in the string */
reverseEachWord(s);
}
};