-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_words.cpp
More file actions
164 lines (133 loc) · 3.84 KB
/
Copy pathreverse_words.cpp
File metadata and controls
164 lines (133 loc) · 3.84 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <catch2/catch_test_macros.hpp>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
namespace LeetCode{
/*README START
# <<Reverse words in a string>>
[Leetcode #151](https://leetcode.com/problems/reverse-words-in-a-string/)
An in-place `O(1)` space solution, beats 94.29% of C++ submissions.

README END
https://leetcode.com/problems/reverse-words-in-a-string/
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
Clarification:
- What constitutes a word?
A sequence of non-space characters constitutes a word.
- Could the input string contain leading or trailing spaces?
Yes. However, your reversed string should not contain leading or trailing spaces.
- How about multiple spaces between two words?
Reduce them to a single space in the reversed string.
*/
class ReverseWordsInString{
private:
inline void reverseWord(char *s, size_t len) {
char c;
size_t i, j;
for (i = 0, j = len - 1; i<j; ++i, --j) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
/* an in-place O(1) space solution:
*
* Example : the sky is blue
* 1) reverse the whole string :
* eulb si yks eht
* 2) move from the start of the string
* 2-1) remember start of a word
* 2-2) find the end of the word
* 2-3) reverse the word
* 3) repeate 2) until the end of the string
*
* also handles extra spaces
*/
size_t _reverseWords(char *s) {
auto len = strlen(s);
char *start = s;
char *end = start + len - 1;
for (; *start == ' '; ++start);
for (; *end == ' '; --end);
if (start > end) {
s[0] = 0;
return 0;
}
auto size = ++end - start;
reverseWord(start, size);
char *p1 = start, *p2 = start;
char *dest = s;
size = 0;
size_t n = 0;
while (p2 <= end) {
++p2;
if (*p2 == ' ' || p2 >= end) {
//p1=>p2-1 is a reversed word
n = p2 - p1;
reverseWord(p1, n);
++n; //space
size += n;
strncpy(dest, p1, n);
dest += n;
//accept one space
++p2;
//if there are more spaces, skip them
for (; *p2 == ' ' && p2 <= end; ++p2);
p1 = p2;
}
}
s[--size] = 0;
return size;
}
public:
void reverseWords(string &s) {
char *p = const_cast<char*>(s.c_str());
s.resize(_reverseWords(p));
}
};
TEST_CASE("Reverse words in string: test cases","[leetcode]") {
ReverseWordsInString t;
SECTION("this sky is blue"){
std::string s1 = "the sky is blue";
const std::string s2 = "blue is sky the";
t.reverseWords(s1);
REQUIRE(s1 == s2);
}
SECTION("all spaces"){
std::string s1 = " ";
const std::string s2 = "";
t.reverseWords(s1);
REQUIRE(s1 == s2);
}
SECTION("one"){
std::string s1 = "one";
const std::string s2 = "one";
t.reverseWords(s1);
REQUIRE(s1 == s2);
}
SECTION("space one") {
std::string s1 = " 1";
const std::string s2 = "1";
t.reverseWords(s1);
REQUIRE(s1 == s2);
}
SECTION("a") {
std::string s1 = "a";
const std::string s2 = "a";
t.reverseWords(s1);
REQUIRE(s1 == s2);
}
SECTION("lots of space in the_middle") {
std::string s1 = " a b ";
const std::string s2 = "b a";
t.reverseWords(s1);
REQUIRE(s1 == s2);
}
}
}