Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def valid_parentheses(expression):
- stack = []
- for symbol in expression:
- if symbol in ['(', '[', '{']:
- stack.append(symbol)
- # if the previous parentheses is a match, then pop that match
- elif stack and (
- (symbol == ')' and stack[-1] == '(') or\
- (symbol == ']' and stack[-1] == '[') or\
- (symbol == '}' and stack[-1] == '{')):
- stack.pop()
- # the stack would be empty if all the parentheses match
- return not stack
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement