class Solution:
def minEditCost(self , str1 , str2 , ic , dc , rc ):
# write code here
dp = [[0] * (len(str2)+1) for _ in range(len(str1)+1)]
for i in range(1, len(str1)+1):
dp[i][0] = dp[i-1][0] + dc
for j in range(1, len(str2)+1):
dp[0][j] = dp[0][j-1] + ic
for i in range(1, len(str1)+1):
for j in range(1, len(str2)+1):
if str1[i-1] == str2[j-1]:
dp[i][j] = min(dp[i-1][j-1],dp[i][j-1]+ic, dp[i-1][j]+dc)
else:
dp[i][j] = min(dp[i-1][j-1]+rc,dp[i][j-1]+ic, dp[i-1][j]+dc)
return dp[-1][-1]
最小编辑代价(牛客)
最新推荐文章于 2021-07-26 16:26:00 发布