20. Valid Parentheses

Easy

Problem:

Determine whether the input value made of parentheses is valid.

Input: s = "()[]{}"
Output: true

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

Solution:

Push (, {, [ onto the stack, and when you encounter ), }, ], check if the result popped from the stack matches the mapping table result.

class Solution:
    def isValid(self, s: str) -> bool:
        stack = []
        table = { ')' : '(', '}': '{', ']' : '['}

        for  char in s:
            if char not in table:
                stack.append(char)
            elif  or table[char] != stack.pop():
                return False
        return len(stack) == 0

Last updated