Skip to content

Commit b9762e2

Browse files
authored
Create remove-outermost-parentheses.cpp
1 parent 1c4c6bb commit b9762e2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

C++/remove-outermost-parentheses.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
string removeOuterParentheses(string S) {
7+
static const int deep = 1;
8+
string result;
9+
int cnt = 0;
10+
for (const auto& c : S) {
11+
if (c == '(' && cnt++ >= deep) {
12+
result += c;
13+
}
14+
if (c == ')' && cnt-- > deep) {
15+
result += c;
16+
}
17+
}
18+
return result;
19+
}
20+
};

0 commit comments

Comments
 (0)