마지막에 len(stack) != 0이라 no가 출력되는 경우를 예제에서 주지 않아서 조금 헤맸다. 코테에서도 예제에서 주지 않은 오류를 잡아야 하는 경우가 많으니까 습관 들여놓자.
import sys
input = sys.stdin.readline
while True:
sen = input().rstrip()
stack = []
temp = False
if (sen == '.'):
break
for s in sen:
if (s == '(' or s == '['):
stack.append(s)
elif s == ')':
if (len(stack) != 0 and stack[-1] == '('):
stack.pop()
else:
temp = True
break
elif s == ']':
if (len(stack) != 0 and stack[-1] == '['):
stack.pop()
else:
temp = True
break
if (len(stack) != 0 or temp):
print("no")
else:
print("yes")
'백준' 카테고리의 다른 글
[백준] 18110번 파이썬 (solved.ac) (0) | 2024.02.24 |
---|---|
[백준] 10989번 파이썬 (수 정렬하기 3) (0) | 2024.02.24 |
[백준] 2231번 파이썬 (분해합) (0) | 2024.02.23 |
[백준] 1966번 파이썬 (프린터 큐) (0) | 2024.02.23 |
[백준] 11866번 파이썬 (요세푸스 문제 0) (0) | 2024.02.22 |