题解 | #编辑距离(二)#

编辑距离(二)

https://www.nowcoder.com/practice/05fed41805ae4394ab6607d0d745c8e4

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# min edit cost
# @param str1 string字符串 the string
# @param str2 string字符串 the string
# @param ic int整型 insert cost
# @param dc int整型 delete cost
# @param rc int整型 replace cost
# @return int整型
#
class Solution:
    def minEditCost(self , str1: str, str2: str, ic: int, dc: int, rc: int) -> int:
        # write code here
        dp = [[0 for _ in range(len(str2) + 1)] for _ in range(len(str1) + 1)]
        for i in range(1, len(str2) + 1):
            dp[0][i] = dp[0][i-1] + ic
        for i in range(1, len(str1) + 1):
            dp[i][0] = dp[i-1][0] + dc
        
        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] = dp[i-1][j-1]
                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]
		要复习

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务