단순한 괄호식 스택 문제
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S):
stack = []
mapping = {')':'(', '}':'{', ']':'['}
for element in S:
if element in '{[(': # 여는 괄호 나올때때
stack.append(element)
else: # 닫는 괄호 나올때
if not stack: # 스택 비어있으면 중첩 문자열 아님
return 0
tmp = stack.pop()
if tmp != mapping[element]:
return 0
element
if stack: # 스택에 남아있음
return 0
else:
return 1