1、Description
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
有效字符串需满足:
- 左括号必须用相同类型的右括号闭合。
- 左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/min-stack
2、Example
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/min-stack
3、Code(Python3)
class Solution:
def isValid(self, s: str) -> bool:
tempList = []
tagDic = {")": "(", "}": "{", "]": "["}
for char in s:
if char in tagDic:
tag = tempList.pop() if tempList else 'tag'
if tagDic[char] != tag:
return False
else:
tempList.append(char)
return not tempList
4、Test