HackerRank Balanced Brackets Solution



 Problem Link :  https://www.hackerrank.com/challenges/balanced-brackets/problem

Solution in C++:

///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat  Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
#include <bits/stdc++.h>
using namespace std;
unordered_map<char,int>symbol={{'[',-3},{'{',-2},{'(',-1},{')',1},{'}',2},{']',3}};
string  Bfunc(string s)
{
    stack<char>st;
   forchar i:s)
    {
        if(symbol[i]<0)
        {
            st.push(i);
        }
        else
        {
            if(st.empty())return "NO";
            else
            {
                char top=st.top();
                st.pop();
                if(symbol[top]+symbol[i]!=0)
                    return "NO";
            }
        }
    }
    if(st.empty())return "YES";
    return "NO";
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        cout<<Bfunc(s)<<endl;
    }
}


No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h ...

Powered by Blogger.