728x90
https://www.acmicpc.net/problem/9012
728x90
내 생각
'('가 나오면 n+=1, ')'가 나오면 n-=1 일 때,
n이 음수가 되거나, 문자열이 끝났는데 아직 양수라면 NO라고 생각했다.
t = int(input())
s = []
ans = []
for i in range(0, t):
s.append(input())
ans.append('NO')
for i in range(0, t):
n = 0
for j in range(0, len(s[i])):
if s[i][j] == '(':
n += 1
elif s[i][j] == ')':
n -= 1
if n < 0:
break
if n == 0:
ans[i] = 'YES'
for i in range(0, t):
print(ans[i])
728x90
'Problem Solving > BOJ' 카테고리의 다른 글
[백준, BOJ 2750] 수 정렬하기 (python) (0) | 2021.05.10 |
---|---|
[백준, BOJ 10828] 스택 (python) (0) | 2021.05.09 |
[백준, BOJ 10946] 랜덤 게임~~~~ (python) (0) | 2021.05.09 |
[백준, BOJ 2798] 블랙잭 (python) (0) | 2021.05.09 |
[백준, BOJ 10872] 팩토리얼 (python) (0) | 2021.05.09 |