class Solution {
public:
/**
*
* @param word1 string字符串
* @param word2 string字符串
* @return int整型
*/
int minDistance(string word1, string word2) {
// write code here
int l1 = word1.size();
int l2 = word2.size();
vector<vector<int>> dp(word1.size() + 1, vector<int>(word2.size() + 1, 0));
for( int i = 0; i <= l1; i++)dp[i][0] = i;
for( int i = 0; i <= l2; i++)dp[0][i] = i;
for( int i = 1; i <= l1; i ++ ){
for( int j = 1; j <= l2; j++ ){
if(word1[i - 1] == word2[j - 1])
dp[i][j] = dp[i - 1][j - 1];
else dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1;
}
}
return dp[l1][l2];
}
};
06-18
213
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-24
08-24