백준[파이썬]
[백준/Python] - 9012번 괄호
코린이 파닥거리기
2025. 4. 5. 14:06
728x90
반응형
SMALL
https://www.acmicpc.net/problem/9012
제출 코드
import sys
input = sys.stdin.readline
N = int(input().strip())
for _ in range(N):
N_list = []
case = input().strip()
state = True
for i in case:
if i == '(':
N_list.append(i)
else:
if N_list:
N_list.pop(-1)
else:
print("NO")
break
else:
if not N_list:
print("YES")
elif N_list:
print("NO")
접근 방법 및 풀이
'('를 입력받으면 N_list에 append한다.
그 후 ')'를 받으면 '('를 삭제한다. 삭제할 때 N_list가 있으면 '('를 삭제하고, 없으면 괄호 문자열이 아니라는 뜻이므로
NO를 출력 후 break한다.
for루프 다 돈 후에도 break없이 끝나면 else조건문 사용하여 N_list가 비었으면 괄호 문자열이라는 뜻이므로 YES출력한다.
그렇지 않으면 NO를 출력한다.
728x90
반응형
LIST