less than 1 minute read

백준링크

자료구조 스택 활용하기

파이썬

while True:
    t = input()
    stack = []
    
    if t=='.':
        break
    
    for i in t:
        if i == '[' or i == '(':
            stack.append(i)
        elif i == ']':
            if len(stack) != 0 and stack[-1] == '[':
                stack.pop()
            else:
                stack.append(']')
                break
        elif i == ')' :
            if len(stack) != 0 and stack[-1] == '(' :
                stack.pop()
            else :
                stack.append(')')
                break
    if len(stack) == 0 :
        print('yes')
    else :
        print('no')

카테고리:

업데이트:

댓글남기기