输出结果
代码实现
def edit_dis(self, str1, str2):
len1 = len(str1)
len2 = len(str2)
dp = np.zeros((len1 + 1, len2 + 1))
for i in range(len1 + 1):
dp[i][0] = i
for j in range(len2 + 1):
dp[0][j] = j
for i in range(1, len1 + 1):
for j in range(1, len2 + 1):
temp = 0 if str1[i - 1] == str2[j - 1] else 1
dp[i][j] = min(dp[i - 1][j - 1] + temp, min(dp[i - 1][j] + 1, dp[i][j - 1] + 1))
return dp[len1][len2]