def is_valid(string): a = '()' b = '{}' c = '[]' while string: if a in string : string = string.replace(a, '') elif b in string : string = string.replace(b, '') elif c in string : string = string.replace(c, '') else: return False return True