-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveOutermostParentheses*
More file actions
57 lines (49 loc) · 954 Bytes
/
Copy pathRemoveOutermostParentheses*
File metadata and controls
57 lines (49 loc) · 954 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**************************************************/
/**************************************************/
class Solution {
public:
string removeOuterParentheses(string S) {
string temp;
int count = 0;
if (S.size() % 2 == 0) {
if (S == "" || S == " ")return " ";
for (int i = 0; i < S.size(); i++) {
if (S[i] == ')') {
count--;
}
if (count > 0) {
temp += S[i];
}
if (S[i] == '(') {
count++;
}
}
}
return temp;
}
};
/////////////////////
string removeOuterParentheses(const string S)
{
int left = 0, right = 0;
stack<char> sc;
string temp;
for (size_t i = 0; i < S.size();i++) {
if (S[i] == '(') {
if (sc.empty() == true) {
left = i + 1;
}
sc.push(S[i]);
}
if(S[i]==')'){
sc.pop();
if (sc.empty() == true) {
right = i ;
}
}
if (sc.empty()) {
temp += S.substr(left, right - left);
}
}
return temp;
}