当前位置:网站首页>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/
边栏推荐
- Cookie&Session
- go实现命令行的工具cli
- File upload and download
- How to use hybrid format to output ISO files? isohybrid:command not found
- FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
- torch. histc
- The value of the second servo encoder is linked to the NC virtual axis of Beifu PLC for display
- Pytorch training deep learning network settings CUDA specified GPU visible
- Error: plug ins declaring extensions or extension points must set the singleton directive to true
- TEC: Knowledge Graph Embedding with Triple Context
猜你喜欢
AfxMessageBox和MessageBox的用法
Server rendering technology JSP
Binary tree god level traversal: Morris traversal
Cygwin的下载和安装配置
报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
Data exchange JSON
[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation
整合阿里云短信的问题:无法从静态上下文中引用非静态方法
Pyramid Scene Parsing Network【PSPNet】论文阅读
Ultimate dolls 2.0 | encapsulation of cloud native delivery
随机推荐
Valid brackets (force deduction 20)
IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?
Appium自动化测试基础--补充:C/S架构和B/S架构说明
Detailed list of errors related to twincat3 ads of Beifu
You cannot right-click F12 to view the source code solution on the web page
[nine day training] content III of the problem solution of leetcode question brushing Report
Asgnet paper and code interpretation 2
Feature Pyramid Networks for Object Detection论文理解
E15 solution for cx5120 controlling Huichuan is620n servo error
The value of the second servo encoder is linked to the NC virtual axis of Beifu PLC for display
Online public network security case nanny level tutorial [reaching out for Party welfare]
详解Spark运行模式(local+standalone+yarn)
Error: plug ins declaring extensions or extension points must set the singleton directive to true
访问阿里云存储的图片URL实现在网页直接预览略缩图而不直接下载
JS daily development tips (continuous update)
torch.histc
How to achieve 0 error (s) and 0 warning (s) in keil5
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
shell脚本使用两个横杠接收外部参数
MFC窗口滚动条用法