-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_parens.py
32 lines (29 loc) · 1009 Bytes
/
remove_parens.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
def removeInvalidParentheses(s):
open = 0
extra_set = set()
balanced = set()
for i in range(len(s)):
if s[i] == '(':
open += 1
elif s[i] == ')':
if open:
open -= 1
else:
extra_group = [i]
for j in range(i):
if s[j] == ')':
extra_group.append(j)
extra_set.add(tuple(extra_group))
for extras in extra_set:
balanced_string = ''
for i in range(len(extras)):
if i == 0:
balanced_string += s[:extras[i]]
elif i == len(extras) - 1:
balanced_string += s[extras[i] + 1:]
else:
balanced_string += s[extras[i - 1] + 1: extras[i]]
balanced.add(balanced_string)
print balanced
str = '()())(()'
removeInvalidParentheses(str)