-
Notifications
You must be signed in to change notification settings - Fork 0
/
557-reverse-words-in-a-string-iii.cpp
42 lines (33 loc) · 1.3 KB
/
557-reverse-words-in-a-string-iii.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
// Title: Reverse Words in a String III
// Description:
// Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
// Link: https://leetcode.com/problems/reverse-words-in-a-string-iii/
// Time complexity: O(n)
// Space complexity: O(1)
class Solution {
public:
std::string reverseWords(std::string s) {
// indexes to mark the begin and the end of a substring
std::size_t i = 0, j = 0;
while (true) {
// find the begin of a substring
while (i != s.length() && s[i] == ' ') ++i;
// stop if there's no more words
if (i == s.length()) break;
// find the end of the substring
j = i; while (j != s.length() && s[j] != ' ') ++j;
// reverse the substring
reverseSubString(s, i, j);
// continue to search for the next word
i = j;
}
return s;
}
void reverseSubString(std::string &s, std::size_t begin, std::size_t end) {
assert (begin != end);
// swap each pair from both sides
for (std::size_t i = begin, j = end - 1; i < j; ++i, --j) {
std::swap(s[i], s[j]);
}
}
};