This repository was archived by the owner on Sep 22, 2021. It is now read-only.

Description
Description of the Problem
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Code
class Solution:
def isValid(self, s: str) -> bool:
stack = []
openbrackets = ('(', '[', '{')
matches = ('()', '[]', '{}')
for c in s:
if c in openbrackets:
stack.append(c)
else:
if len(stack) < 1 or stack[-1] + c not in matches:
return False
stack.pop()
return len(stack) == 0
Link To The LeetCode Problem
LeetCode