Posted on

Description

Submission

#include <bits/stdc++.h>

using namespace std;

// Complete the isBalanced function below.
string isBalanced(string s) {
    stack<char> stk;
    for(char c : s) {
        if(stk.empty()) {
            stk.push(c);
            continue;
        }
        if((c == '}' && stk.top() == '{') || 
        (c == ']' && stk.top() == '[') || 
        (c == ')' && stk.top() == '(') ) {
            stk.pop();
        } else {
            stk.push(c);
        }
    }
    if(stk.empty()) return "YES";
    return "NO";
}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));

    int t;
    cin >> t;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    for (int t_itr = 0; t_itr < t; t_itr++) {
        string s;
        getline(cin, s);

        string result = isBalanced(s);

        fout << result << "\n";
    }

    fout.close();

    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *