
문제링크: https://www.acmicpc.net/problem/9012
문자열 문제이다. "(" 와 ")" 개수를 +1, -1 로 계산해주고 음수가 되면 "NO"를 출력하게 만들었다.
import sys
def find_answer(st):
cnt = 0
for s in st:
if s == "(":
cnt += 1
if s == ")":
cnt -= 1
if cnt < 0:
print("NO")
return
if cnt == 0:
print("YES")
else:
print("NO")
T = int(sys.stdin.readline())
for _ in range(T):
st = sys.stdin.readline().strip()
find_answer(st)