import java.util.*; public class Solution { public int editDistance (String str1, String str2) { int n = str1.length() , m = str2.length(); int[][] dp = new int[n+1][m+1]; for(int i = 1; i <= n; i ++){ dp[i][0] = i; } for(int j = 1; j <= m; j ++){ dp[0][j] = j; } for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ int l = dp[i-1][j] + 1; // 删除 int u = dp[i][j-1] + 1; // 插入 int lu = dp[i-1][j-1]; // 修改 if(str1.charAt(i-1) != str2.charAt(j-1)){ lu += 1; } dp[i][j] = Math.min(Math.min(l,u),lu); } } return dp[n][m]; } }