Problem Solutions

20. Valid Parentheses

class Solution:
    def isValid(self, s: str) -> bool:
        if len(s) <=  1:
            return False
        stack = []
        dictionary = {"}": "{", "]": "[", ")": "("}
        for item in s:
            if item in ["{", "[", "("]:
                stack.append(item)
            elif item in dictionary:
                if not stack or stack.pop() != dictionary[item]:
                    return False
        return len(stack) == 0
Edit this script Updated at Tue, Dec 12, 2023