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;
for( char 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