题解 | #计算字符串的编辑距离#

计算字符串的编辑距离

https://www.nowcoder.com/practice/3959837097c7413a961a135d7104c314

import sys

origin = input()
target = input()

dp = [[0 for _ in range(0, len(target)+1)] for _ in range(0, len(origin)+1)]
dp[0] = list(range(0, len(target)+1))
for i in range(0, len(origin) + 1):
    dp[i][0] = i
for i in range(1, len(origin)+1):
    for j in range(1, len(target)+1):
        if target[j-1] == origin[i-1]:
            dp[i][j] = dp[i-1][j-1]
        else:
            dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1
# print(dp)
print(dp[len(origin)][len(target)])

全部评论

相关推荐

10-16 09:58
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务