#include
#include
using namespace std;
string check(string s)
{
stack st;
for(int i=0;i<s.length();i++)
{
if(s[i]==β(β||s[i]==β{β||s[i]==β[β)
{
st.push(s[i]);
}
else
{
switch(s[i])
{
case β)β:
if(st.empty()||st.top()!=β(β)
return βNOβ;
st.pop();
break;
case β}β:
if(st.empty()||st.top()!=β{β)
return βNOβ;
st.pop();
break;
if(st.empty()||st.top()!=β[β)
return βNOβ;
st.pop();
break;
}
}
}
return s.empty()?βYESβ:βNOβ;
}
int main() {
string s;
cin>>s;
check(s);
return 0;
}
My code gets compiled successfully but is not givinfg answer.Can u please find the mistake.
Getting no answer
- Line 35: What are you supposed to do with the return value of your function?
- Line 30: Why are you returning βs.empty()β?
- Line 24: Where is the default case of your switch?
But the same problem was on hackerrank.I used the same code for that.It worked there but not working on CB IDE
Can u please correct my mistake in the code.
@Hk199977,
I can but that would benefit absolutely no one, I have actually tried submitting the above code after making the changes I recommended and it works just fine.
Try debugging them given the exact locations of the bugs, and donβt pay much attention to results of other platforms, if thereβs a WA, thereβs a bug, just find it.
BTW one more heads up, You arenβt handling switch cases well, you are not using βbreakβ statement, despite the given cases being mutually exclusive.