본문 바로가기
백준

[백준] 4949번 파이썬 (균형잡힌 세상)

by 헤이즐넛 좋아하는 개발자 2024. 2. 23.

마지막에 len(stack) != 0이라 no가 출력되는 경우를 예제에서 주지 않아서 조금 헤맸다. 코테에서도 예제에서 주지 않은 오류를 잡아야 하는 경우가 많으니까 습관 들여놓자.

import sys
input = sys.stdin.readline

while True:
    sen = input().rstrip()
    stack = []
    temp = False

    if (sen == '.'):
        break
    for s in sen:
        if (s == '(' or s == '['):
            stack.append(s)
        elif s == ')':
            if (len(stack) != 0 and stack[-1] == '('):
                stack.pop()
            else:
                temp = True
                break
        elif s == ']':
            if (len(stack) != 0 and stack[-1] == '['):
                stack.pop()
            else:
                temp = True
                break
    if (len(stack) != 0 or temp):
        print("no")
    else:
        print("yes")