-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathremove-outermost-parentheses.py
45 lines (30 loc) · 1.1 KB
/
remove-outermost-parentheses.py
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
import unittest
class Solution:
def removeOuterParentheses(self, S):
stack = []
result = ""
for s in S:
if not (len(stack) == 0 or len(stack) == 1 and s == ")"):
result += s
if s == "(":
stack.append("(")
elif s == ")":
stack.pop()
return result
class TestSolution(unittest.TestCase):
def setUp(self):
self.sol = Solution()
def test_empty(self):
self.assertEqual(self.sol.removeOuterParentheses(""), "")
def test_one(self):
self.assertEqual(self.sol.removeOuterParentheses("()"), "")
def test_two1(self):
self.assertEqual(self.sol.removeOuterParentheses("(())"), "()")
def test_two2(self):
self.assertEqual(self.sol.removeOuterParentheses("()()"), "")
def test_custom1(self):
self.assertEqual(self.sol.removeOuterParentheses("(()())(())"), "()()()")
def test_custom2(self):
self.assertEqual(self.sol.removeOuterParentheses("(()())(())(()(()))"), "()()()()(())")
if __name__ == "__main__":
unittest.main()