解题思路:
1.将s中{},[],()能用"“替换的进行替换
2.当s长度大于s_new长度说明未替换完毕继续替换,知道无法替换为止
3.当最终s_new为空时利用python中None, False, 空字符串”", 0, 空列表[], 空字典{}, 空元组()都相当于False,有具体值时相当于True,即此时的s_new未全部进行""替换,最后通过not s_new 输出具体结果。
class Solution:
def isValid(self, s: str) -> bool:
s_new = s.replace('()','').replace('[]','').replace('{}','')
while len(s_new) < len(s):
s = s_new
s_new = s.replace('()','').replace('[]','').replace('{}','')
return not s_new#在python中 None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()都相当于False: