Check out example codes for "balanced brackets hackerrank solution in cpp". It will help you in understanding the concepts better.

Code Example 1

#include <iostream>
#include <algorithm>
#include <unordered_map>
#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;
            default: 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;
}

Learn ReactJs, React Native from akashmittal.com