본문 바로가기

자료구조와 알고리즘

20. Valid Parentheses(유효한 괄호)

class Solution:
    def isValid(self, s: str) -> bool:

        stack = []
        table = {
            ")":"(",
            "]":"[",
            "}":"{"
        }
        
        for char in s:
            if char not in table:
                stack.append(char)
            elif not stack or table[char] != stack.pop():
                return False
        
        return len(stack) == 0