본문 바로가기
백준

[백준] 9012번 파이썬 (괄호)

by 헤이즐넛 좋아하는 개발자 2024. 2. 22.

규칙을 정리해보면 아래와 같다.

이를 코드로 구현해서 완성하였다.

import sys
input = sys.stdin.readline

n = int(input().rstrip())

for _ in range(n):
    ps = list(input().rstrip())
    i = 1
    while (i != len(ps) and len(ps) != 0):
        if (ps[0] == ')'):
            break
        if (ps[i] == ')'):
            ps.pop(i)
            ps.pop(i-1)
            i = 1
        else:
            i += 1
    if (len(ps) == 0):
        print("YES")
    else:
        print("NO")