Same test case gives different result while testing and submitting


#1

A=“CC”
B="?"
its correct output is zero and on running it gives ans as zero but on submitting it gives ans as 1
here is my code…what is the problem in my code?

unordered_map<string,int> mp;
bool solve(string &a,string &b,int n,int m)
{
if(n==0 && m==0)
{
return true;
}
if(n==0 || m==0)
{
return false;
}
string pat=to_string(n)+"-"+to_string(m);
if(mp.find(pat)!=mp.end())
{
return mp[pat];
}
if(a[n-1]==b[m-1])
{
return mp[pat]=solve(a,b,n-1,m-1);
}
else
{
if(b[m-1]!=’*’ && b[m-1]!=’?’)
{
return mp[pat]=false;
}
else if(b[m-1]==’?’)
{
return mp[pat]=solve(a,b,n-1,m-1);
}
else
{
return mp[pat]=(solve(a,b,n,m-1)||solve(a,b,n-1,m));
}
}
}
int Solution::isMatch(const string A, const string B) {
string a="$"+A+"$";
string b="$"+B+"$";
//cout<<a<<" “<<b<<”\n";
// memset(dp,-1,sizeof(dp));
if(solve(a,b,a.length(),b.length()))
{
return 1;
}
return 0;
}


#2

you are getting this problem due to the global defining of your map.
In multiple test cases, your map isn’t empty at starting from test case 2,
so better call mp.clear(), before calling solve function in isMatch function.

PS: I know you must have solved this or moved to next one, but keep this thing in mind whenever defining some global variables.