• + 19 comments

    C++ solution.

    #include <stack>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    string isBalanced(string s) {
        stack<char> st;  
        
        for (auto c: s) {
            switch (c) {
                case '{':
                case '(':
                case '[':
                    st.push(c);
                    break;
                case '}':
                    if (st.empty() || (st.top() != '{')) {
                        return "NO";
                    }
                    st.pop();
                    break;
                case ')':
                    if (st.empty() || (st.top() != '(')) {
                        return "NO";
                    }
                    st.pop();
                    break;
                case ']':
                    if (st.empty() || (st.top() != '[')) {
                        return "NO";
                    }
                    st.pop();
                    break;
            }
        }
        
        return st.empty() ? "YES" : "NO";
    }
    
    
    int main(){
        int t;
        cin >> t;
        for(int a0 = 0; a0 < t; a0++){
            string s;
            cin >> s;
            cout << isBalanced(s) << endl;
        }
        return 0;
    }