백준 9012 - 괄호 (파이썬)

Updated:

Answer

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

for _ in range(n):
    a = sys.stdin.readline()
    cnt = 0

    for i in a:
        if i == '(':
            cnt += 1
        if i == ')':
            if cnt == 0:
                cnt = 1
                break
            else:
                cnt -= 1

    if cnt:
        print('NO')
    else:
        print('YES')

Categories:

Updated: