백준 9012

김당찬·2022년 4월 22일
0
  • 문제 : https://www.acmicpc.net/problem/9012
  • 아이디어 : 스택 구조 흉내내기(여는 괄호일 때 stack, 닫는 괄호일 때 pop >> 개수가 남거나 부족하면 NO)
# 핵심 : 스태킹 !

import sys
T = int(sys.stdin.readline())

for t in range(T):
    x = list(sys.stdin.readline().strip())
    ls = []
    earlystop = False
    for i, val in enumerate(x):
        if val == '(':
            ls.append(i)
        elif len(ls) == 0:
            earlystop = True
            break
        else:
            ls.pop()
    if earlystop:
        print('NO')
    else:
        print('YES' if len(ls) == 0 else "NO")
profile
블로그 이사했습니다 https://ddangchani.github.io

0개의 댓글