-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreverse_words.cpp
52 lines (44 loc) · 1.08 KB
/
reverse_words.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
43
44
45
46
47
48
49
50
51
52
// Reverse Words
/*
Given a String str, reverse the string without reversing its individual words. Words are separated by dots.
Note: The last character has not been '.'.
Example1:
Input: str = i.like.this.program.very.much
Output: much.very.program.this.like.i
Example2:
Input: str = pqr.mno
Output: mno.pqr
Expected Time Complexity: O(|str|)
Expected Auxiliary Space: O(|str|)
Constraints:
1 <= |str| <= 10^5
*/
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string reverseWords(string str) {
int i = 0, j = 0, n = str.length();
auto beginItr = str.begin();
while(j < n) {
while(j < n && str[j] != '.')
j++;
reverse(beginItr + i, beginItr + j);
j++;
i = j;
}
reverse(beginItr + i, beginItr + j);
reverse(beginItr, beginItr + n);
return str;
}
};
int main() {
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
Solution obj;
cout << obj.reverseWords(s) << endl;
}
}