Can someone help me to find out why my code is giving wrong answers :
Question link :: here
Code ::
class Solution {
bool isSubSequence(string str1, string str2,int m,int n)
{
if (m == 0)
return true;
if (n == 0)
return false;
if (str1[m - 1] == str2[n - 1])
return isSubSequence(str1, str2, m - 1, n - 1);
return isSubSequence(str1, str2, m, n - 1);
}
int dp[1009];
int solve(vector<string>&a,int in,string s)
{
if(in == a.size())
return 0;
if(dp[in]!=-1)
return dp[in];
if(s.size()==0)
return dp[in]=max(1+solve(a,in+1,a[in]),solve(a,in+1,s));
string s1=a[in];
if((isSubSequence(s, s1,s.size(),s1.size())==true)&&(s1.size()-s.size()==1))
{
// cout<<s<<" "<<a[in]<<endl;
return dp[in]=max(1+solve(a,in+1,a[in]),solve(a,in+1,s));
}
else
{
return dp[in]=solve(a,in+1,s);
}
}
static bool bylength(string &s1, string &s2){
return s1.length()<s2.length();
}
public:
int longestStrChain(vector& a) {
sort(a.begin(), a.end(),bylength);
int n=a.size(),i;
//for(i=0;i<n;i++)cout<<a[i]<<" ";
memset(dp,-1,sizeof(dp));
string s="";
int x=solve(a,0,s);
return x;
}
};