본문 바로가기
알고리즘/BOJ

[BOJ / 백준] 4949번 균형잡힌 세상 python / 파이썬 스택

by seohmoon 2022. 3. 9.


 
 

 


9012번 괄호랑 비슷해서 여기서 코드 수정 함

[BOJ / 백준] 9012번 괄호 python / 파이썬 스택 (tistory.com)

 

 

스택을 사용해서 풀어줌 

 

 

1) "(" 일 때

2) ")" 일 때

3) "[" 일 때

4) "]" 일 때

 

while True:
    arr = list(map(str, input()))
    if len(arr) == 1 and arr[0] == ".":
        break
    stack = []
    result = 0
    for j in arr:
        if j == '(':
            stack.append(j)
        elif j == ')':
            if stack != []:
                a = stack.pop()
                if a == '[':
                    result = 1
                    break
            elif stack == []:
                result = 1
                break
        elif j == '[':
            stack.append(j)
        elif j == ']':
            if stack != []:
                b = stack.pop()
                if b == '(':
                    result = 1
                    break
            elif stack == []:
                result = 1
                break
    if result == 1:
        print("no")
    elif stack != []:
        print("no")
    elif result == 0 and stack == []:
        print("yes")

 

 

댓글