본문 바로가기

ALGORITHM/Algorithm 문제풀이

[문제풀이] Stack - 균형잡힌 세상

 

 

1차 시도


while True:
    input_data = input()
    if input_data == '.':
        break
    input_data = input_data.replace(' ', "")
    stack = []
    for character in input_data:
        if character == '(' or character == '[':
            stack.append(character)
        elif character == ')':
            if len(stack) != 0:
                last = stack.pop()
                if last != '(':
                    print("no")
                    stack = None
                    break
            else:
                print("no")
                stack = None
                break
        elif character == ']':
            if len(stack) != 0:
                last = stack.pop()
                if last != '[':
                    print("no")
                    stack = None
                    break
            else:
                print("no")
                stack = None
                break
    if stack is not None:
        if len(stack) == 0:
            print("yes")
        else:
            print("no")

스택 문제는 좀 뻔하다(?)