728x90
Stack을 pop 했을 때 top의 값이 감소하는 것을 이용하여 풀었습니다.
코드
import sys
n = int(sys.stdin.readline())
for i in range(n) :
stack = sys.stdin.readline()
top = 0
for j in stack:
if j == '(' :
top += 1
elif j == ')' :
top -= 1
if top < 0 :
print('NO')
break
if top > 0 :
print('NO')
elif top == 0:
print('YES')
입출력
'Algorithm' 카테고리의 다른 글
[알고리즘] 덱 백준 10866 (Python) (0) | 2022.01.16 |
---|---|
[알고리즘] 큐 백준 10845 (Python) (0) | 2022.01.14 |
[알고리즘] 스택 백준 10828 (Python) (0) | 2022.01.14 |
[알고리즘] K번째 수 백준 11004 (Python) (0) | 2022.01.14 |
[알고리즘] 카드 백준 11652 (Python) (0) | 2022.01.14 |