#!/usr/bin/python # -*- coding: UTF-8 -*-
from pythonds.basic.stack import Stack
def parChecker(symbolString): s = Stack() balanced = True index = 0 while index < len(symbolString) and balanced: symbol =symbolString[index]
#左边括号入栈 if symbol == '(': s.push(symbol) else:
#如果栈提前为空,则表示前面匹配成功,后面没有匹配成功 if s.isEmpty(): balanced = False
#右边括号出栈 else: s.pop()
index += 1
if balanced and s.isEmpty(): return True else: return False print(parChecker('()())')) print(parChecker('()()()')) print(parChecker('((()())())'))