有效的括号

https://leetcode.cn/problems/valid-parentheses/

给定一个只包括(){}[]的字符串s,判断字符串是否有效。

有效字符串需满足: - 左括号必须用相同类型的右括号闭合。 - 左括号必须以正确的顺序闭合。 - 每个右括号都有一个对应的相同类型的左括号。

示例1

输入:s = "()"
输出:true

示例2

输入:s = "()[]{}"
输出:true

示例3

输入:s = "(]"
输出:false

示例4

输入:s = "([])"
输出:true

示例5

输入:s = "([)]"
输出:false

提示 - 1 <= s.length <= 10^4 - s仅由括号()[]{}组成

class Solution:
    def isValid(self, s: str) -> bool:
        stack = []
        for item in s:
            if len(stack) == 0:
                stack.append(item)
                continue
            if item == ')' and stack[-1] == '(':
                stack = stack[:-1]
            elif item == '}' and stack[-1] == '{':
                stack = stack[:-1]
            elif item == ']' and stack[-1] == '[':
                stack = stack[:-1]
            else:
                stack.append(item)
        return len(stack) == 0