Skip to content

Commit 3627d85

Browse files
authored
Create remove-outermost-parentheses.py
1 parent b9762e2 commit 3627d85

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def removeOuterParentheses(self, S):
6+
"""
7+
:type S: str
8+
:rtype: str
9+
"""
10+
deep = 1
11+
result, cnt = [], 0
12+
for c in S:
13+
if c == '(' and cnt >= deep:
14+
result.append(c)
15+
if c == ')' and cnt > deep:
16+
result.append(c)
17+
cnt += 1 if c == '(' else -1
18+
return "".join(result)

0 commit comments

Comments
 (0)