Parentheses pairing ({}[]()<>) issue

前端 未结 9 1544
醉酒成梦
醉酒成梦 2021-02-06 00:27

I want to be able to pair up all parentheses in a string, if they aren\'t paired then then they get their index number and False. It seems like it is repeating some values over

9条回答
  •  隐瞒了意图╮
    2021-02-06 00:59

    Try this:

    def matched(s):
    stack=[]
    open,close="(",")" 
    for i in s:
        if i in open:
            stack.append(i)
        if i in close:
            if len(stack)==0:
                return(False)
            else:   
                stack.pop()
    if len(stack):
        return(False)
    else:
        return(True)
    

提交回复
热议问题