--- tags: data_structure_python --- # Valid Parentheses <img src="https://img.shields.io/badge/-easy-brightgreen"> Given a string containing just the characters $($, $)$, $\{$, $\}$, $[$ and $]$, determine if the input string is valid. An input string is valid if: ``` 1. Open brackets must be closed by the same type of brackets. 2. Open brackets must be closed in the correct order. ``` Note that an empty string is also considered valid. <ins>**Example 1:**</ins> >Input: "()" >Output: true <ins>**Example 2:**</ins> >Input: "()[]{}" >Output: true <ins>**Example 3:**</ins> >Input: "(]" >Output: false <ins>**Example 4:**</ins> >Input: "([)]" >Output: false <ins>**Example 5:**</ins> >Input: "{[]}" >Output: true ## Solution ```python= class Solution: def isValid(self, s: str) -> bool: Map = {'}': '{', ']': '[', ')': '('} stack = [] for char in s: if char not in Map: stack.append(char) else: if len(stack) == 0 or stack[-1] != Map[char]: return False stack.pop(-1) return len(stack) == 0 ```