classSolution { public: intminDistance(string word1, string word2) { // Let dp[i][j] represent the minimum number of operations required to convert the first i characters of word1 to the first j characters of word2. int i, j; int m = word1.length(), n = word2.length(); vector<vector<int> > dp(m + 1, vector<int>(n + 1, 0)); for (i = 0; i <= m; i++) { // Changing word1[0..=i] to an empty string requires i operations dp[i][0] = i; }
for (j = 0; j <= n; j++) { // Changing an empty string to word2[0..=j] requires j operations dp[0][j] = j; }
for (i = 1; i <= m; i++) { for (j = 1; j <= n; j++) { if (word1[i - 1] == word2[j - 1]) { // No additional operation needed dp[i][j] = dp[i - 1][j - 1]; } else {