Easy to understand simple java solution


#1

public class Solution {
public int minDistance(String A, String B) {
int [][]memo = new int[A.length()+1][B.length()+1];

    populate(memo,A,B);
    
    // for(int i = 0;i<=A.length();i++){
    //         System.out.println("");
    //     for(int j = 0;j<=B.length();j++){
    //         System.out.print(" "+memo[i][j]);
    //     }
    // }
    
    return memo[A.length()][B.length()];
}
public void populate(int [][]memo,String A,String B){
    memo[0][0]=0;
    for(int i =1;i<=B.length();i++){
        memo[0][i]=i;
    }
    
    for(int i =1;i<A.length();i++){

          memo[i][0]=i;

    }
    for(int i = 1;i<=A.length();i++){
        for(int j = 1;j<=B.length();j++){
            if(A.charAt(i-1)==B.charAt(j-1)){
                memo[i][j]=memo[i-1][j-1];
            }
            else{
                int min = Math.min(memo[i-1][j],memo[i-1][j-1]);
                min = Math.min(min,memo[i][j-1]);
                memo[i][j] = min+1;
            }
        }
    }
}

}