Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First recursive solution in Clear category for Remove Brackets by Lemur21
D = {'(': ')', '{': '}', '[': ']'}
def remove_brackets(line: str) -> str:
if len(line) < 2:
return ''
if line[0] in ']})':
return remove_brackets(line[1:])
s = remove_brackets(line[1:])
ridx = 0
while(True):
ridx = line.find(D[line[0]], ridx+1)
if ridx < 0: break
s2 = line[0] + remove_brackets(line[1:ridx]) + line[ridx] \
+ remove_brackets(line[ridx+1:])
if len(s2) > len(s): s = s2
return s
if __name__ == '__main__':
#print("Example:")
print(remove_brackets('()()'))
assert remove_brackets('(()()') == '()()'
assert remove_brackets('[[{}()]]') == '[[{}()]]'
#These "asserts" are used for self-checking and not for an auto-testing
assert remove_brackets('[][[[') == '[]'
assert remove_brackets('[[(}]]') == '[[]]'
assert remove_brackets('[[[[[[') == ''
assert remove_brackets('[[[[}') == ''
assert remove_brackets('') == ''
assert remove_brackets('[(])') == '()'
print("Coding complete? Click 'Check' to earn cool rewards!")
Dec. 18, 2020