You are viewing a single comment's thread. Return to all comments →
Python 3 solution
def isBalanced(s): op = [] for i in range(len(s)): if s[i] in '({[': op.append(s[i]) elif s[i] in ')}]': try: if op[-1]+s[i] == '()' or op[-1]+s[i] == '{}' or op[-1]+s[i] == '[]': op.pop(-1) else: return 'NO' except: return 'NO' if len(op) != 0: return 'NO' else: return 'YES'
Seems like cookies are disabled on this browser, please enable them to open this website
Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
Python 3 solution