[Leetcode]20. Valid Parentheses

2024. 8. 1. 18:33Algorithm

1. problem :

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

 

2. solution 1 :

class Solution:
    def isValid(self, s: str) -> bool:
        hashMap = {')':'(',']':'[','}':'{'}
        open_brackets = ['(','[','{']
        temp_stack = []
        for i in s:
            if i in open_brackets:
                temp_stack.append(i) 
            else:
                if not temp_stack or hashMap[i] != temp_stack.pop():
                    return False 
         
        return not temp_stack