https://www.acmicpc.net/problem/2504

내가 짠 코드

import sys

str = sys.stdin.readline().strip()
stk = []
arr = [[] for i in range(30)]

depth = -1
for i in range (len(str)) :
    temp = str[i]
    
    if len(stk) == 0 : #스택에 하나도 없는 경우
        stk.append(temp)
        depth += 1
    
    elif stk[-1] == '(' and temp == ')': # 스택에 () 만나는경우
        if(len(arr[depth+1]) == 0) :
            arr[depth].append(2)
        else :
            sum_val = 0
            for it in arr[depth+1] :
                sum_val += it
            arr[depth+1] = []
            arr[depth].append(2*sum_val)
        stk.pop()
        depth -= 1
    
    elif stk[-1] == '[' and temp == ']': # 스택에 [] 만나는경우
        if(len(arr[depth+1]) == 0) :
            arr[depth].append(3)
        else :
            sum_val = 0
            for it in arr[depth+1] :
                sum_val += it
            arr[depth+1] = []
            arr[depth].append(3*sum_val)
        stk.pop()
        depth -= 1
    
    else :
        stk.append(temp)
        depth += 1

    # print(depth)
    # print(arr)
    
if len(stk) != 0 :
    print(0)
else :
    sum_val = 0
    for it in arr[0] :
        sum_val += it
    print(sum_val)

다른 사람이 짠 코드

bracket = list(input())

stack = []
answer = 0
tmp = 1

for i in range(len(bracket)):

    if bracket[i] == "(":
        stack.append(bracket[i])
        tmp *= 2

    elif bracket[i] == "[":
        stack.append(bracket[i])
        tmp *= 3

    elif bracket[i] == ")":
        if not stack or stack[-1] == "[":
            answer = 0
            break
        if bracket[i-1] == "(":
            answer += tmp
        stack.pop()
        tmp //= 2

    else:
        if not stack or stack[-1] == "(":
            answer = 0
            break
        if bracket[i-1] == "[":
            answer += tmp

        stack.pop()
        tmp //= 3

if stack:
    print(0)
else:
    print(answer)

참고

https://hongcoding.tistory.com/114