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 925 (Div. 3) 1931D. Divisible Pairs Solution

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

Powered by Blogger.