문제 바로가기> 백준 4949번: 균형잡힌 세상
import sys
while True:
stack = []
flag = True
str_line = sys.stdin.readline().rstrip()
if str_line == ".":
break
else:
for i in str_line:
if i=='(' or i=='[':
stack.append(i)
elif i==')':
if len(stack)==0 or stack[-1]!='(':
flag = False
break
else:
stack.pop()
elif i==']':
if len(stack)==0 or stack[-1]!='[':
flag = False
break
else:
stack.pop()
if len(stack)==0 and flag:
print("yes")
else:
print("no")
if len(stack)==0 and flag:
print("yes")
else:
print("no")
# 아래와 같이도 작성 가능
from sys import stdin, stdout
stdout.write("yes\n" if len(stack)==0 and flag else "no\n")