# -*- coding:utf-8 -*- class ChkExpression: def chkLegal(self, A): # write code here BL = set('([{') BR = set('}])') stk = [] for a in A: if a in BR: if a == ')': b = '(' if a == ']': b = '[' if a == '}': b = '{' if stk[-1]!=b: return False stk.pop() if a in BL: stk.append(a) return not len(stk)