YouTube上非常直观的视频: https://www.youtube.com/watch?v=xFd5P9nyhTw
public class Solution {
public int minDistance(String word1, String word2) {
int len1 = word1.length();
int len2 = word2.length();
int[][] f = new int[len1 + 1][len2 + 1];
for (int i = 0; i <= len1; i++) {
f[i][0] = i;
}
for (int j = 0; j <= len2; j++) {
f[0][j] = j;
}
for (int i = 1; i <= len1; i++) {
char a = word1.charAt(i - 1);
for (int j = 1; j <= len2; j++) {
char b = word2.charAt(j - 1);
if (a == b) {
f[i][j] = f[i - 1][j - 1];
} else {
int replace = f[i - 1][j - 1];
int insert = f[i][j - 1];
int delete = f[i - 1][j];
f[i][j] = Math.min(replace, Math.min(insert, delete)) + 1;
}
}
}
return f[len1][len2];
}
}