You are viewing a single comment's thread. Return to all comments →
Python 3 dictionary solution:
def is_matched(expression): brackets = {'{': 1, '}': -1, '[': 2, ']': -2, '(': 4, ')': -4} stack = [] for i in expression: if stack and stack[-1] + brackets[i] == 0: stack.pop() elif brackets[i] > 0: stack.append(brackets[i]) else: return False return not stack
Seems like cookies are disabled on this browser, please enable them to open this website
Stacks: Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
Python 3 dictionary solution: