#include <iostream>
#include <string>
#include <stack>
#include <algorithm>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int res=0,x=1;
    string s;
    cin >> s;
    stack<char> sta;

    for(int i=0; s[i]; i++) {
        
        if(s[i]=='(') {
            x*=2;
            sta.push(s[i]);
        } else if(s[i]=='[') {
            x*=3;
            sta.push(s[i]);
        } else if (s[i]==')') {
            if (sta.empty() || sta.top()!='(')  {
                res=0; 
                break;
            }
            else if(s[i-1]=='('){
                res+=x;
                x/=2;
                sta.pop();
            } else {
                x/=2;
                sta.pop();
            }
        } else if (s[i]==']') {
            if (sta.empty() || sta.top()!='[')  {
                res=0; 
                break;
            }
            else if(s[i-1]=='['){
                res+=x;
                x/=3;
                sta.pop();
            } else {
                x/=3;
                sta.pop();
            }
        }
     
    }
    if(sta.empty()) cout << res;
    else cout << 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: