Untitled
raw download clone
TEXT
views 26
,
size 1151 b
#include <iostream>
#include <stack>
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;
    cin.ignore();
    for (int i = 0; i < t; i++) {
        string s;
        getline(cin, s);

        string result = isBalanced(s);

        cout << result << "\n";
    }
    return 0;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.