9012번 : 괄호

김민관·2022년 9월 13일

백준_Silver

목록 보기
35/57

문제보기

파이썬

n = int(input())

for i in range(n):
    stack = list(input())
    temp = []

    for i in stack:
        if i == '(':
            temp.append(i)
        else:
            if temp and temp[-1] == '(':
                temp.pop()
            else:
                temp.append(i)

    if temp:
        print("NO")
    else:
        print("YES")

풀이

  • '('일때는 일단 temp에 추가
  • ')'일때 기존에 temp의 마지막이 '('이면 없애기
  • 마지막에 temp가 비워져있으면 yes
profile
게임 개발일지 & IT 소식들 공유

0개의 댓글