给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
class Solution:
def isValid(self, s: str) -> bool:
if len(s)%2 ==1:
return False
dic={")":"(","}":"{","]":"[",}
stack=[]
for ch in s:
if ch in dic:
if not stack or stack[-1] != dic[ch]:
return False
stack.pop()
else:
stack.append(ch)
return not stack