Skip to content

Valid Parentheses

20. Valid Parentheses

class Solution:
    def isValid(self, s: str) -> bool:
        if len(s) % 2 != 0:
            return False
        stack = []
        left = ["(", "{", "["]
        right = [")", "}", "]"]
        for char in s:
            if len(stack) == 0 or char in left:
                stack.append(char)
            else:
                if stack[-1] == left[right.index(char)]:
                    stack.pop()
                else:
                    return False
        return len(stack) == 0

Reference:

  1. Blind Curated 75

Comments