有效括号-正确的顺序闭合
'''
有效括号:
左括号必须以正确的顺序闭合
正确【】{} ()
错误 [{]}]
'''
class Solution:
def is_valid(self,s):
dic = {'(':')','[':']','{':'}'}
stack = []
for i in s:
if i in dic.keys():
stack.append(i)
elif stack:
if dic[stack[-1]]==i:
stack.pop(-1)
else:
return False
else:
return False
if len(stack) == 0:
return True
else:
return False
s = ['[',']','{']
r = Solution().is_valid(s)
print(r)