아무리봐도 맞는 것 같은데 출력초과가 떠서 구글링해보니 sys.stdin.readline이 문제라더라. 시간 단축을 위해선 무조건 readline을 써야 한다고 생각했는데 아니었다. 그 이유를 추측해봤을 땐 아래와 같다.
import sys
sentence = input()
while sentence != '.':
sentence = list(sentence.split())
stack = []
for i in sentence:
if i.isalpha():
continue
else:
for j in i:
if j == ')':
if stack and stack[-1] == '(':
stack.pop()
else:
stack.append(j)
break
elif j == ']':
if stack and stack[-1] == '[':
stack.pop()
else:
stack.append(j)
break
elif j == '(' or j == '[':
stack.append(j)
if not stack:
print("yes")
else:
print("no")
sentence = input()

import sys
sentence = sys.stdin.readline().rstrip()
while sentence != '.':
sentence = list(sentence.split())
stack = []
for i in sentence:
if i.isalpha():
continue
else:
for j in i:
if j == ')':
if stack and stack[-1] == '(':
stack.pop()
else:
stack.append(j)
break
elif j == ']':
if stack and stack[-1] == '[':
stack.pop()
else:
stack.append(j)
break
elif j == '(' or j == '[':
stack.append(j)
if not stack:
print("yes")
else:
print("no")
sentence = sys.stdin.readline().rstrip()
