728x90

< 해결 방법 >

이미 접한 적이 있던 문제라 쉽게 풀었다. test 라는 변수를 만들어서 이 값이 음수가 되면 False 를 반환하는 것이 중요하다.

 

 

import sys
 
a = int(sys.stdin.readline())
 
def check(k):
    if k.count("(")!=k.count(")"):
        return False
 
    test = 0 
 
    for i in range(0,len(k)):
        if k[i]=="(":
            test+=1
            if test<0:
                return False
        else:
            test-=1   
            if test<0:
                return False
 
    return True             
 
 
 
 
for i in range(0,a):
    k=list(sys.stdin.readline().rstrip())
    if check(k)==True:
        print("YES")
    else:
        print("NO")    
728x90

+ Recent posts