解题思路:
成对删除括号,直到序列长度不变,当序列为空时,合法
'''
# @param s string字符串
# @return bool布尔型
#
def fun(s):
t = [len(s),-1]
while t[0] != t[1]:
s = s.replace('()','')
s = s.replace('[]','')
s = s.replace('{}','')
t.insert(0,len(s))
t.pop()
return s
class Solution:
def isValid(self , s ):
# write code here
s2 = fun(s)
if not s2:
return True
else:
return False
京公网安备 11010502036488号