백준 Python 9012 괄호

Seohyun·2023년 8월 3일

알고리즘

목록 보기
14/36

문제 링크

import sys

t = int(sys.stdin.readline())

for _ in range(t):
    
    stack = []
    
    st = sys.stdin.readline().rstrip()

    for i in st:
        
        if i == '(':
            stack.append(i)
        elif i == ')':
            if stack:
                stack.pop()
            else:
                stack.append(i)
                break
    
    if stack:
        print('NO')
    else:
        print('YES')
profile
Hail hamster

0개의 댓글