백준 7585번: Brackets #Python

ColorlessDia·2024년 7월 23일

algorithm/baekjoon

목록 보기
247/808
import sys
from collections import deque

matched_bracket = dict(zip(list(')}]'), list('({[')))

while True:
    line = sys.stdin.readline().rstrip()

    if line == '#':
        break
    
    stack = deque([])

    for char in line:
        
        if char not in '({[]})':
            continue
        
        if char in '({[':
            stack.append(char)
            continue
        
        if char in ']})':
            stack.append(char)
    
        if matched_bracket[char] == stack[-2]:
            stack.pop()
            stack.pop()
            
    if len(stack) == 0:
        print('Legal')
    else:
        print('Illegal')

0개의 댓글