Tle for m*n solution how this is possible


#1

code
int Solution::numDistinct(string A, string B)
{
int n=A.length();
int m=B.length();
int dp[m+1][n+1];
int i,j=0;
for(i=0;i<=m;i++){dp[i][0]=0;}
for(j=0;i<=n;j++){dp[0][j]=1;}
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{ dp[i][j]=0;
if(B.at(i-1)!=A.at(j-1)){dp[i][j]=dp[i][j-1];}
else {dp[i][j]=dp[i-1][j-1]+dp[i][j-1]; }
}
}
return dp[m][n];
}


#2

Check this statement
for(j=0;i<=n;j++){dp[0][j]=1;}
it should be
for(j=0;j<=n;j++){dp[0][j]=1;}

This wrong condition was giving you TLE.