[Python] 프로그래머스 - Level2 - 올바른 괄호

강주형·2022년 8월 20일
0

https://school.programmers.co.kr/learn/courses/30/lessons/12909

스택/큐

def solution(s):
    stack = []

    for i in s:
        if not stack:
            stack.append(i)
        else:
            if stack[-1] == '(' and i == ')':
                stack.pop()
            else:
                stack.append(i)
    if stack:
        return False
    return True

바로 앞에서 한 짝지어 제거하기랑 매우 유사해서 금방 풀었다!

profile
Statistics & Data Science

0개의 댓글