Very Simple Approach (Beginner friendly)


#1

very simple approach: Commented where needed…

int Solution::minDistance(string A, string B) {
    int n=A.size();
    int m=B.size();
    
    int dp[n+1][m+1]={};
    memset(dp, 0, sizeof dp);
    

    dp[0][0]=0; // our basecase.
    // if A is empty then we need i insertion
    for(int i=1; i<=m; i++)
        dp[0][i]=i;

    // if B is empty we delete i char
    for(int i=1; i<=n; i++)
        dp[i][0]=i;
        
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            if(A[i-1]!=B[j-1]){ // char not same.

               // take min of prev insert delete replace add 1 for current op.
                dp[i][j]=1+min({dp[i-1][j], dp[i-1][j-1], dp[i][j-1]});

            }
            else
                dp[i][j]=dp[i-1][j-1]; // if cur char same choose prev bast ans.
        }
    }
    
    return dp[n][m];
}

Take DP array of +1 side because we store state + base case as well.