Why is this code wrong>


#1

int Solution::minDistance(string A, string B) {

int m=B.size(),n=A.size();
int T[m+1][n+1];
for(int i=0;i<n+1;i++)
{
    for(int j=0;j<m+1;j++)
    {
        if(i==0 && j==0)
        T[i][j]=0;
        else if(i==0)
        T[i][j]=T[i][j-1]+1;
        else if(j==0)
        T[i][j]=T[i-1][j]+1;
        else
        {
            if(A[i-1]==B[i-1])
            T[i][j]=T[i-1][j-1];
            else
            T[i][j] = 1+min(min(T[i][j-1],T[i-1][j]),T[i-1][j-1]);
            
        }
    }
}
return T[n][m];

}


#2

int Solution::minDistance(string A, string B) {
int m=A.size(),n=B.size();
int T[451][451];

for(int i=0;i<m+1;i++)
{
for(int j=0;j<n+1;j++)
{
if(i==0 && j==0)
T[i][j]=0;
else if(i==0)
T[i][j]=j; // changed this part
else if(j==0)
T[i][j]=i; // and this , didn’t check yours, maybe doing the same work.
else
{
if(A[i-1]==B[j-1]) // you were using A[i-1]==B[i-1] (B[i-1]which should be B[j-1])
T[i][j]=T[i-1][j-1];
else
T[i][j] = 1+min(min(T[i][j-1],T[i-1][j]),T[i-1][j-1]);

    }
}

}
return T[m][n];
}