forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_199.py
31 lines (25 loc) · 869 Bytes
/
problem_199.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
def correct_braces(braces):
# get rid of all initial closing braces
i = 0
while i < len(braces) and braces[i] == ')':
i += 1
braces = braces[i:]
# base case for recursion
if not braces:
return ''
# check for the first balanced group of braces
open_braces = 0
for i, brace in enumerate(braces):
if brace == '(':
open_braces += 1
elif brace == ')':
open_braces -= 1
if not open_braces:
break
# if there is one, process the rest separately, else truncate the excess opening braces
return braces[open_braces:] if open_braces else braces[:i+1] + correct_braces(braces[i+1:])
# Tests
assert correct_braces("()(()") == "()()"
assert correct_braces("()(()))") == "()(())"
assert correct_braces(")(())") == "(())"
assert correct_braces("())(") == "()"