2 test cases not passing how to fix it?
hello @akash_281
check now->
#include<iostream>
using namespace std;
#include<stack>
bool balance(string s)
{
stack<char> s1;
for(int i=0;s[i]!='\0';i++)
{
if(s[i]=='(')
{
s1.push(s[i]);
}
if(s[i]==')')
{ if(s1.empty()){ // added: if stack is empty then return false
return false;
}
if(s1.top()=='(')
{
s1.pop();
}
}
}
if(s1.empty())
{
return true;
}
return false;
}
int main()
{
string s;
cin>>s;
balance(s);
if(balance)
{
cout<<"Yes";
}
else
{
cout<<"No";
}
return 0;
}
Still not passing all the test cases
in input we can have other brackets as well( u r handling only for () ).
check this for refrence->