SHyeonL / Future_Internet_LAB_Algorithm

미래 인터넷 연구실 알고리즘 스터디
0 stars 0 forks source link

9012 - 괄호 #5

Open SHyeonL opened 1 year ago

SHyeonL commented 1 year ago
import sys

input = sys.stdin.readline

n = int(input())
stack = []
for i in range(n):
    x = list(input().strip())
    for j in range(len(x)):
        if x[j] == '(':
            stack.append(x[j])
        else:
            if not stack:
                print('NO')
                break
            stack.pop()
        if j == len(x) - 1:
            if stack:
                print('NO')
            else:
                print('YES')
            stack.clear()
yuneojin commented 1 year ago
T = int(input())

for i in range(T):
    string = input()
    cnt = 0
    s = []
    for j in string:
        if j == "(":
            s.append(j)
            cnt += 1
        else:
            if len(s)!=0:
                s.pop()
                cnt += 1
            else:
                break

    if len(s)==0 and cnt == len(string):
        print("YES")
    else:
        print("NO")