关于括号合规这个函数怎么写

判断一个字符串s,s仅为三种括号,判断括号的使用是否合规,输出True,Flase
题目来自codewars。

"(){}[]"   =>  True
"([{}])"   =>  True
"(}"       =>  False
"[(])"     =>  False
"[({})](]" =>  False
"[({})][]" =>  True
"[({})()][]" =>  True

def validBraces(s):
  while '{}' in s or '()' in s or '[]' in s:
      s=s.replace('{}','')
      s=s.replace('[]','')
      s=s.replace('()','')
  return s==''

img

li = {"(": ")", "[": "]", "{": "}"}
def jugde():
    while True:
        s = input("输入括号>>").strip()
        if s == "":
            print("请输入括号")
            continue
        for i in s:
            if i not in li.values() and i not in li.keys():
                print(f"{i}:该括号不存在")
                break
        for n,i in enumerate(s):
            if i in li.keys():
                v = li.get(i)
                i_index = n
                v_index = s.find(v,n)
                if v_index == -1:
                    print(False)
                    break
                if (v_index-i_index)<0 or not (v_index-i_index-1) % 2 == 0:
                    print(False)
                    break
            elif i in li.values():
                i_index = s.find(i)
                new_s = s[0:i_index]
                for k,v in li.items():
                    if v == i:
                        new_s_index = new_s.rfind(k)
                        if new_s_index == -1:
                            print(False)
                            break
                        if not (i_index-new_s_index-1) % 2 == 0:
                            print(False)
                            break
        else:
            print(True)