给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。
class Solution:
def isValid(self, s: str) -> bool:
hashmap = {'(':')','[':']','{':'}'}
stack = []
for c in s:
if c in hashmap:
stack.append(c)
else:
if stack:
top = stack[-1]
if c != hashmap[top]:
return False
else:
stack.pop()
else:
return False
return not stack