20-Valid Parentheses

Title: judging large, medium and small parenthesis is legitimate. "()", "([)]", "{(([])}"

def isValid(string):
    dic = {'}':'{',']':'[',')':'('}
    stack = []
    for s in string:
        if s not in dic:
            stack.append(s)
        else:
            if len(stack)>0 and dic[s]==stack[-1]:
                stack.pop()
            else:
                return False

    return True

  

Guess you like

Origin www.cnblogs.com/kingshine007/p/11366033.html