Need some help on Balance Bracket

I was solving some stack problem on Hacker Rank and I got stuck on this one
quetion
solution
My solution passed all test cases except 1(test case 9).
Fun fact: I made more than 5 submissions on this question in the span of 8 months and I’m still missing that one case. Can you help me?

Can you please share your code using pastebin or by any other means because the solution link is not accessible?

Code:

/* Date:03-05-2021 */
/* Time:11:52:50 */
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long int
#define ld long double
#define rep(i,a,n) for(ll i=a;i<n;i++)
#define per(i,n,a) for(ll i=n-1;i>=a;i--)
#define rep1(i,a,n) for(ll i=a;i<=n;i++)
#define forit(it,x) for(auto it=(x).begin();it!=(x).end();++it)
#define all(x) (x).begin(),(x).end()
#define prec(n) fixed<<setprecision(n)
#define testcases ll t; cin>>t; while(t--)
#define pi 3.1415926535897932384626433832795
#define mp(x,y) make_pair(x,y)
#define vii  vector<int>
#define pii  pair<int,int>
#define pll pair<ll,ll>
#define mpii map<int,int>
#define vll vector<ll>
#define mpll map<ll,ll>
#define vpi  vector<pii>
#define umpll unordered_map<ll,ll>
#define pqi  priority_queue<int>
#define umpii unordered_map<int,int>
#define pb push_back
#define eb emplace_back
const ll mod=(ll)1e9+9;
const ll maxi=(ll)1e18+123;
//For Debugging
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
//Number(n) of Digits in base(b)=floor(logb(n))+1
//char to integer (char-'0')
//Integer to char (char+'0')
void solve(){
    string str;cin>>str;
    stack<char>stk;
    for(int i=0;i<(int)str.size();i++){
        if(stk.empty()){
            if(str[i]=='(' || str[i]=='{' || str[i]=='['){
                stk.push(str[i]);
            }
        }else if(str[i]==')' && stk.top()=='('){
            stk.pop();
        }else if(str[i]=='}' && stk.top()=='{'){
            stk.pop();
        }else if(str[i]==']' && stk.top()=='['){
            stk.pop();
        }else{
            stk.push(str[i]);
        }
    }
    if(!stk.empty()){
        cout<<"NO\n";
    }else{
        cout<<"YES\n";
    }
}
int main(){
    IOS;
    testcases{
       solve();
    }
}

Consider the test input:

1 
)()
2 Likes

thanks

1 Like