给定两个字符串str1和str2,再给定三个整数ic,dc和rc,分别代表插入、删除和替换一个字符的代价,请输出将str1编辑成str2的最小代价。
数据范围:,
要求:空间复杂度 ,时间复杂度
class Solution: def minEditCost(self , str1: str, str2: str, ic: int, dc: int, rc: int) -> int: # write code here m = len(str1) n = len(str2) dp = [[0 for j in range(n + 1)] for i in range(m + 1)] for i in range(1, m + 1): dp[i][0] = dp[i - 1][0] + dc for j in range(1, n + 1): dp[0][j] = dp[0][j - 1] + ic for i in range(1, m + 1): for j in range(1, n + 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] + dc, dp[i][j - 1] + ic, dp[i - 1][j - 1] + rc) return dp[-1][-1]二维动态规划