解答
动态规划
class Solution {
public:
int minDistance(string word1, string word2) {
int m = word1.size();
int n = word2.size();
if(m==0||n==0) return max(m,n);
vector<vector<int>> dp(m+1,vector<int>(n+1));
dp[0][0] = 0;
for (int i = 1; i < m+1; i++)
{
dp[i][0] = i;
}
for (int j = 1; j < n+1; j++)
{
dp[0][j] = j;
}
for (int i = 1; i < m+1; i++)
{
for (int j = 1; j < n+1; j++)
{
if(word1[i-1]==word2[j-1])
{
dp[i][j] = min(dp[i-1][j-1],min(dp[i-1][j],dp[i][j-1])+1);
}
else
{
dp[i][j] = min(dp[i-1][j-1],min(dp[i-1][j],dp[i][j-1]))+1;
}
}
}
return dp[m][n];
}
};
|