forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1021.cpp
More file actions
34 lines (24 loc) · 828 Bytes
/
1021.cpp
File metadata and controls
34 lines (24 loc) · 828 Bytes
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
class Solution {
public:
string removeOuterParentheses(string s) {
stack<char> s1;
vector<int> points;
// loop to find endpoints of valid parantheses strings
for(int i=0; i<s.size(); i++){
if(s[i] == '('){
if(s1.empty())
points.push_back(i);
s1.push(s[i]);
}
else if(s[i] == ')'){
s1.pop();
if(s1.empty())
points.push_back(i);
}
}
string result;
for(int i=0; i<points.size(); i += 2)
result += s.substr(points[i] + 1, points[i + 1] - points[i] - 1);
return result;
}
};