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

前端 未结 9 1575
醉酒成梦
醉酒成梦 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:50

    BRACES = { '(': ')', '[': ']', '{': '}' }
    
    def group_check(s):
        stack = []
        for b in s:
            c = BRACES.get(b)
            if c:
                stack.append(c)
            elif not stack or stack.pop() != b:
                return False
        return not stack
    

提交回复
热议问题