public class Solution {
public int minDistance(String word1, String word2) {
int m = word1.length();
int n = word2.length();
int[][] dist = new int[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
dist[i][0] = i;
}
for (int j = 0; j <= n; j++) {
dist[0][j] = j;
}
for (int i = 1; i <= m; i++) {
char charA = word1.charAt(i - 1);
for (int j = 1; j <= n; j++) {
char charB = word2.charAt(j - 1);
if (charA == charB) {
dist[i][j] = dist[i - 1][j - 1];
} else {
int replace = dist[i - 1][j - 1];
int insert = dist[i][j - 1];
int delete = dist[i - 1][j];
dist[i][j] = Math.min(replace, Math.min(insert, delete)) + 1;
}
}
}
return dist[m][n];
}
}