一、题目
二、代码
class Solution
{
public int minDistance(String word1, String word2)
{
int i,j;
int length_1,length_2;
length_1 = word1.length();
length_2 = word2.length();
int[][] dp = new int[length_1+1][length_2+1];
for(i=0;i<=length_1;i++)
{
dp[i][0] = i;
}
for(j=0;j<=length_2;j++)
{
dp[0][j] = j;
}
for(i=1;i<=length_1;i++)
{
for(j=1;j<=length_2;j++)
{
if(word1.charAt(i-1) == word2.charAt(j-1))
{
dp[i][j] = dp[i-1][j-1];
}
else
{
dp[i][j] = 1+ Math.min(dp[i-1][j-1],Math.min(dp[i-1][j],dp[i][j-1]));
}
}
}
return dp[length_1][length_2];
}
}
三、运行结果