import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param str1 string字符串 * @param str2 string字符串 * @return int整型 */ public int editDistance (String str1, String str2) { // write code here if (str1 == null || str2 == null) { return 0; } char[] arr1 = str1.toCharArray(); char[] arr2 = str2.toCharArray(); int n = arr1.length; int m = arr2.length; int[][] dp = new int[n + 1][m + 1]; for (int i = 1; i <= n; i++) { dp[i][0] = dp[i - 1][0] + 1; } for (int j = 1; j <= m; j++) { dp[0][j] = dp[0][j - 1] + 1; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (arr1[i - 1] == arr2[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; } else { dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])) + 1; } } } return dp[n][m]; } }