class Solution:
def isValid(self, s: str) -> bool:
while "()" in s or "{}" in s or '[]' in s:
s = s.replace("()", "").replace('{}', "").replace('[]', "")
return s == ''
method 2: stack 学了数据结构
classSolution:defisValid(self, s:str)->bool:
stack =[]for c in s:if c =='{'or c =='['or c =='(':
stack.append(c)elif stack and stack[-1]=='{'and c =='}':
stack.pop()elif stack and stack[-1]=='['and c ==']':
stack.pop()elif stack and stack[-1]=='('and c ==')':
stack.pop()else:returnFalsereturnlen(stack)==0