[LeetCode] Valid Parentheses (Python)

2024. 3. 6. 13:43알고리즘

 

 

class Solution:
    def isValid(self, s: str) -> bool:
        stack=[]
        brackets={'}':'{',')':'(',']':'['}
        for bracket in s:
            if bracket in brackets.values(): 
                stack.append(bracket)
            else:
                if stack and brackets[bracket]==stack[-1] :  
                    stack.pop()
                else: 
                    return False
        
        if stack:
            return False
        return True

'알고리즘' 카테고리의 다른 글

[LeetCode] Target Sum (Python)  (0) 2024.03.07
[LeetCode] Daily Temperatures (Python)  (0) 2024.03.06
[백준 13458] 시험감독 (Python)  (0) 2024.03.05
[LeetCode] Perfect Squares (Python)  (0) 2024.03.05
[LeetCode] Open the Lock (Python)  (0) 2024.03.05