forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
41 lines (29 loc) · 774 Bytes
/
main.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
/// Source : https://leetcode.com/problems/remove-outermost-parentheses/
/// Author : liuyubobobo
/// Time : 2019-04-06
#include <iostream>
#include <stack>
using namespace std;
/// Using Stack
/// Time Complexity: O(|s|)
/// Space Complexity: O(1)
class Solution {
public:
string removeOuterParentheses(string S) {
string res = "";
int stack = 1, start = 0;
for(int i = 1; i < S.size(); i ++){
if(S[i] == '(') stack ++;
else stack --;
if(stack == 0){
res += S.substr(start + 1, i - start + 1 - 2);
start = i + 1;
}
}
return res;
}
};
int main() {
cout << Solution().removeOuterParentheses("(()())(())") << endl;
return 0;
}