题解 | #表达式合法判断#
表达式合法判断
https://www.nowcoder.com/practice/227893ccf81d4e8589875922f0d9319e
# -*- 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)

