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.
- Every close bracket has a corresponding open bracket of the same type.
class Solution:
def isValid(self, s: str) -> bool:
stack = []
close_to_open = { ")": "(", "]": "[", "}": "{"}
for char in s:
is_closing_bracket = char in close_to_open
if is_closing_bracket:
open_bracket = close_to_open[char]
if len(stack) == 0 or stack.pop() != open_bracket:
return False
else:
stack.append(char)
return len(stack) == 0
If the current character is a closing bracket, check that the character on top of che stack is the relative opening bracket.