Java Solution for Edit Distance


#1

public class Solution {
public int minDistance(String A, String B) {
int l1 = A.length();
int l2 = B.length();
int mat[][] = new int[l1+1][l2+1];
mat[0][0] = 0;
for(int i=1;i<l1+1;i++){
mat[i][0] = i;
}
for(int i=1;i<l2+1;i++){
mat[0][i] = i;
}

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

}