Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Remove Brackets by tokiojapan55
BRACKETS = '(){}[]'
def parse(a, depth):
result = ''
e = len(a) - 1
while e > 0:
t = BRACKETS.index(a[e])
if t % 2:
for s in range(e - 1, -1, -1):
if a[s] == BRACKETS[t - 1] and depth[s] == depth[e]:
result = a[s] + parse(a[s+1:e], depth[s+1:e]) + a[e] + result
e = s
break
e -= 1
return result
def remove_brackets(a):
work = [0, 0, 0]
depth = []
for c in a:
t = BRACKETS.index(c)
work[t // 2] += 1 if not t % 2 else -1
depth.append(work[t // 2] + (t % 2))
return parse(a, depth)
June 17, 2020