-
Notifications
You must be signed in to change notification settings - Fork 118
/
1190. Reverse Substrings Between Each Pair of Parentheses.cpp
68 lines (62 loc) · 2.28 KB
/
1190. Reverse Substrings Between Each Pair of Parentheses.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//Stack, Brute Force
//https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/383670/JavaC%2B%2BPython-Why-not-O(N)
//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Reverse Substrings Between Each Pair of Parentheses.
//Memory Usage: 6.4 MB, less than 100.00% of C++ online submissions for Reverse Substrings Between Each Pair of Parentheses.
//time: O(N^2), space: O(N)
class Solution {
public:
string reverseParentheses(string s) {
stack<int> openIdxs;
string ans = "";
for(int i = 0; i < s.size(); i++){
switch(s[i]){
case '(':{
openIdxs.push(ans.size());
break;
}case ')':{
int openIdx = openIdxs.top(); openIdxs.pop();
reverse(ans.begin() + openIdx, ans.end());
break;
}default:{
ans += s[i];
break;
}
}
}
return ans;
}
};
//Wormholes, two-pass
//https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/383670/JavaC%2B%2BPython-Why-not-O(N)
//Runtime: 4 ms, faster than 56.64% of C++ online submissions for Reverse Substrings Between Each Pair of Parentheses.
//Memory Usage: 6.7 MB, less than 100.00% of C++ online submissions for Reverse Substrings Between Each Pair of Parentheses.
//time: O(N), space: O(N)
class Solution {
public:
string reverseParentheses(string s) {
int N = s.size();
string ans = "";
stack<int> openIdxs;
map<int, int> pair;
for(int i = 0; i < N; i++){
if(s[i] == '('){
openIdxs.push(i);
}else if(s[i] == ')'){
int j = openIdxs.top(); openIdxs.pop();
pair[i] = j;
pair[j] = i;
}
}
for(int i = 0, d = 1; i < N; i += d){
if(s[i] == '(' || s[i] == ')'){
i = pair[i];
d = -d;
// cout << i << " " << pair[i] << " " << d << endl;
}else{
ans += s[i];
}
// cout << i << " " << ans << endl;
}
return ans;
}
};