Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
itertools.combinations solution in Clear category for Remove Brackets by David_Jones
from itertools import combinations
BRACKETS = {
'(' : ')',
'[' : ']',
'{' : '}'
}
def is_balanced(s):
stack = ['']
for ch in s:
if ch in BRACKETS:
stack.append(BRACKETS[ch])
elif ch in BRACKETS.values() and ch != stack.pop():
return False
return stack == ['']
def remove_brackets(a):
for length in reversed(range(len(a) + 1)):
for combination in reversed(tuple(combinations(a, length))):
if is_balanced(combination):
return ''.join(combination)
June 4, 2019
Comments: