-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path301.Remove-Invalid-Parentheses.py
62 lines (48 loc) · 1.79 KB
/
301.Remove-Invalid-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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# https://leetcode.com/problems/remove-invalid-parentheses/
#
# algorithms
# Hard (40.13%)
# Total Accepted: 141,220
# Total Submissions: 351,940
class Solution(object):
def removeInvalidParentheses(self, s):
"""
:type s: str
:rtype: List[str]
"""
length = len(s)
left_rem, right_rem = 0, 0
for ch in s:
if ch == '(':
left_rem += 1
elif ch == ')':
if left_rem > 0:
left_rem -= 1
else:
right_rem += 1
hash_map = {}
def recursive(idx, left_brackets, right_brackets, left_rem, right_rem, path):
if idx == length:
if left_rem == 0 and right_rem == 0:
hash_map[''.join(path)] = True
return
if s[idx] == '(' and left_rem > 0:
recursive(idx + 1, left_brackets, right_brackets,
left_rem - 1, right_rem, path)
if s[idx] == ')' and right_rem > 0:
recursive(idx + 1, left_brackets, right_brackets,
left_rem, right_rem - 1, path)
path.append(s[idx])
if s[idx] == '(':
recursive(idx + 1, left_brackets + 1,
right_brackets, left_rem, right_rem, path)
elif s[idx] == ')':
if right_brackets < left_brackets:
recursive(idx + 1, left_brackets,
right_brackets + 1, left_rem, right_rem, path)
else:
recursive(idx + 1, left_brackets, right_brackets,
left_rem, right_rem, path)
path.pop()
recursive(0, 0, 0, left_rem, right_rem, [])
return hash_map.keys()