• + 1 comment

    My Python submission gives RuntimeError or WrongAnswer in most of the test cases. Anyone knows why?

    def isBalanced(s):
        # Write your code here
        assert len(s) >= 1 and len(s) <= 1e3
        # print(1e3 == 1000)
        
        pairs = {
            "{": "}",
            "(": ")",
            "[": "]"
        }
        brackets = ('{', '}', '(', ')', '[', ']')
        o_brackets = ('{', '(', '[')
        # c_brackets = ('}', ')', ']')
        
        while len(s):
            # print(len(s))
            for c_idx, c in enumerate(s):
                assert c in brackets
                if c in o_brackets:
                    o_c = c
                else:
                    if pairs[o_c] == c:
                        s = s[:c_idx-1]+s[c_idx+1:]
                        break
                    else:
                        return "NO"
        # print(len(s))
        return "YES"