forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_086.py
28 lines (20 loc) · 844 Bytes
/
problem_086.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
def get_chars_removed_helper(string, stack, removed):
if not string and not stack:
return removed
elif not string:
return len(stack) + removed
if string[0] == ')' and stack and stack[-1] == '(':
stack.pop()
return get_chars_removed_helper(string[1:], stack, removed)
removed_chars_add = get_chars_removed_helper(
string[1:], stack + [string[0]], removed)
removed_chars_ignore = get_chars_removed_helper(
string[1:], stack, removed + 1)
return min(removed_chars_add, removed_chars_ignore)
def get_chars_removed(string):
chars_removed = get_chars_removed_helper(string, list(), 0)
return chars_removed
assert get_chars_removed("()())()") == 1
assert get_chars_removed(")(") == 2
assert get_chars_removed("()(((") == 3
assert get_chars_removed(")()(((") == 4