You are viewing a single comment's thread. Return to all comments →
def isBalanced(s): open_brackets = ['{', '[', '('] close_brackets = ['}', ']', ')'] q = [] for bracket in s: if bracket in open_brackets: q.append(bracket) else: try: open_bracket = q.pop() if open_brackets.index(open_bracket) - close_brackets.index(bracket) != 0: return 'NO' except Exception: return 'NO' return 'YES' if len(q) == 0 else 'NO'
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 →