当前位置:网站首页>72. 编辑距离
72. 编辑距离
2022-07-01 03:23:00 【Sun_Sky_Sea】
72. 编辑距离
原始题目链接:https://leetcode.cn/problems/edit-distance/
给你两个单词 word1 和 word2, 请返回将 word1 转换成 word2 所使用的最少操作数 。
你可以对一个单词进行如下三种操作:
插入一个字符
删除一个字符
替换一个字符
示例 1:
输入:word1 = “horse”, word2 = “ros”
输出:3
解释:
horse -> rorse (将 ‘h’ 替换为 ‘r’)
rorse -> rose (删除 ‘r’)
rose -> ros (删除 ‘e’)
示例 2:
输入:word1 = “intention”, word2 = “execution”
输出:5
解释:
intention -> inention (删除 ‘t’)
inention -> enention (将 ‘i’ 替换为 ‘e’)
enention -> exention (将 ‘n’ 替换为 ‘x’)
exention -> exection (将 ‘n’ 替换为 ‘c’)
exection -> execution (插入 ‘u’)
提示:
0 <= word1.length, word2.length <= 500
word1 和 word2 由小写英文字母组成
解题思路:
定义dp[i][j]的含义为:word1的前i个字符和word2的前j个字符的编辑距离。就是word1的前i个字符,变成word2的前j个字符,最少需要这么多步。如果其中一个字符串是空串,那么编辑距离是另一个字符串的长度;两个串都不空时,dp[i][j]=min(dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+int(word1[i]!=word2[j])):
例如,word1 = “abcde”, word2 =“fgh”,我们现在算这俩字符串的编辑距离,就是找从word1,最少多少步,能变成word2?那就有三种方式:知道"abcd"变成"fgh"多少步(假设X步),那么从"abcde"到"fgh"就是"abcde"->“abcd”->“fgh”。(一次删除,加X步,总共X+1步)
知道"abcde"变成“fg”多少步(假设Y步),那么从"abcde"到"fgh"就是"abcde"->“fg”->“fgh”。(先Y步,再一次添加,加X步,总共Y+1步)
知道"abcd"变成“fg”多少步(假设Z步),那么从"abcde"到"fgh"就是"abcde"->“fge”->“fgh”(替换)。
代码实现:
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
m, n = len(word1), len(word2)
if m * n == 0:
return m + n
# 定义dp[i][j]:word1到i的字符 变换成 word2[j] 的编辑距离
dp = [[0] * (n + 1) for _ in range(m + 1)]
# 初始化dp:word1为空或者word2为空,那么转换的最少步数
# 就是word1或word2的长度
for i in range(m + 1):
dp[i][0] = i
for j in range(n + 1):
dp[0][j] = j
# 根据dp[i - 1][j] dp[i][j - 1] dp[i - 1][j - 1]
# 计算dp[i][j]的值
for i in range(1, m + 1):
for j in range(1, n + 1):
dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + int(word1[i - 1] != word2[j - 1]))
return dp[m][n]
参考文献:
https://leetcode.cn/problems/edit-distance/solution/bian-ji-ju-chi-by-leetcode-solution/
边栏推荐
- 【伸手党福利】JSONObject转String保留空字段
- JS日常开发小技巧(持续更新)
- pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear
- bootsrap中的栅格系统
- The combination of applet container technology and IOT
- [small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation
- 过滤器 Filter
- Overview of EtherCAT principle
- Appium automation test foundation -- supplement: c/s architecture and b/s architecture description
- IPv4 and IPv6, LAN and WAN, gateway, public IP and private IP, IP address, subnet mask, network segment, network number, host number, network address, host address, and IP segment / number - what does
猜你喜欢

岭回归和lasso回归

Appium automation test foundation -- supplement: c/s architecture and b/s architecture description

bootsrap中的栅格系统

二叉树神级遍历:Morris遍历

Detailed list of errors related to twincat3 ads of Beifu

实现pow(x,n)函数

Error: plug ins declaring extensions or extension points must set the singleton directive to true

Pytorch training deep learning network settings CUDA specified GPU visible

在线公网安备案保姆级教程【伸手党福利】

Test function in pychram
随机推荐
FCN全卷积网络理解及代码实现(来自pytorch官方实现)
LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
Cygwin的下载和安装配置
How to use hybrid format to output ISO files? isohybrid:command not found
TEC: Knowledge Graph Embedding with Triple Context
The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list
Blueprism registration, download and install -rpa Chapter 1
完全背包问题
multiple linear regression
go实现命令行的工具cli
静态库使用MFC和共享库使用MFC的区别
Leetcode: offer 59 - I. maximum value of sliding window
Depth first traversal of C implementation Diagram -- non recursive code
BluePrism注册下载并安装-RPA第一章
Implement pow (x, n) function
[daily training] 1175 Prime permutation
在 C 中声明函数之前调用函数会发生什么?
Pathmeasure implements loading animation
C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display
Overview of EtherCAT principle