[백준] 하루5문제(25.02.27)

HAHAHELLO·2025년 3월 7일

파이썬

목록 보기
28/50

스택, 큐, 덱

4949: 균형잡힌 세상

문제

예제

나의 풀이

밸런스 부분을 고려하지 못해서 푸는 데 시간이 꽤 걸렸다. 나중에 다시 풀어볼 예정

while True:
    line = input().rstrip()
    if line == '.':
        break
    stack = []
    balance = True
        
    for i in line:
        if i in '([':
            stack.append(i)
        elif i in '])':
            if not stack:
                balance = False
                break
            stack_pop = stack.pop()
            if (stack_pop == '[' and i != ']') or (stack_pop == '(' and i != ')'):
                balance = False
    if balance and not stack:
        print('yes')
    else:
        print('no')
profile
데이터 엔지니어가 되어 봅시다 🌈

0개의 댓글