Valid Parentheses in Python

class Solution:
    def isValid(self, s: str) -> bool:
        # Stack to store opening brackets
        stack = []
        # Dictionary to map closing brackets to their corresponding opening brackets
        bracket_map = {")": "(", "}": "{", "]": "["}

        # Iterate through each character in the string
        for char in s:
            if char in ["(", "{", "["]:
                # If it's an opening bracket, push to the stack
                stack.append(char)
            else:
                # If it's a closing bracket, check the top of the stack
                if not stack or stack.pop() != bracket_map[char]:
                    # If stack is empty or brackets don't match, return False
                    return False

        # If stack is empty, all brackets were matched, return True; else False
        return len(stack) == 0

Next