blob: 2c3953682bae543a9647671a30266f24d85e3846 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
from collections import deque
def check_expression(exp):
if len(exp) & 1:
return False
stack = deque()
for ch in exp:
if ch == '(' or ch == '{' or ch == '[':
stack.append(ch)
if ch == ')' or ch == '}' or ch == ']':
if not stack:
return False
top = stack.pop()
if (top == '(' and ch != ')') or (top == '{' and ch != '}' or (top == '[' and ch != ']')):
return False
return not stack
|