728x90
반응형
class Solution:
    def isValid(self, s: str) -> bool:
        stack = []
        table = {
            ')': '(',
            '}': '{',
            ']': '[',
        }
        
        for i in s:
            if i not in table:
                stack.append(i)
            elif not stack or table[i] != stack.pop():
                return False 
            
        return len(stack) == 0 

 

입력된 괄호 식이 올바른지 판단하는 문제. 

테이블에 없는 값이 들어오면 스택에 넣고 있는 값이 들어오면 스택에서 꺼내서 값이 맞는지 확인한다. 

딕셔너리 자료형을 활용해서 풀이한다. 

 

leetcode.com/problems/valid-parentheses/

 

Valid Parentheses - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

728x90
반응형

+ Recent posts