classSolution { public: int dp[1005][1005]; intminDistance(string a, string b){ int n = a.size(); int m = b.size(); for (int i = 0; i <= n; i++) dp[i][0] = i; for (int i = 0; i <= m; i++) dp[0][i] = i; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int x = dp[i - 1][j] + 1; // 在 b[j] 后面插入一个字符 a[i] int y = dp[i][j - 1] + 1; // 在 a[i] 后面插入一个字符 b[j] int z = dp[i - 1][j - 1] + (a[i - 1] != b[j - 1]); // 修改一个字符 dp[i][j] = min({x, y, z}); } } return dp[n][m]; } };