[알고리즘/백준] 9012: 괄호(python)

유현민·2022년 4월 4일
0

알고리즘

목록 보기
90/253

스택을 이용해서 풀었다. 닫는 괄호가 나오면 스택에 넣고 여는 괄호가 나오면 스택에서 닫는 괄호를 pop 해주었다.

from sys import stdin
N = int(stdin.readline())
tmp = []
for i in range(N):
    a = list(map(str, stdin.readline().strip()))
    if len(a) % 2 == 0:
        for j in range(len(a)):
            if a[-1] == ')':
                tmp.append(a.pop())
            elif len(tmp) > 0 and a[-1] == '(':
                tmp.pop()
                a.pop()
        if len(tmp) or len(a):
            print('NO')
        else:
            print('YES')
        tmp = []
    else:
        print('NO')
profile
smilegate megaport infra

0개의 댓글