当前位置:网站首页>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/
边栏推荐
- pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear
- [reach out to Party welfare] developer reload system sequence
- 整合阿里云短信的问题:无法从静态上下文中引用非静态方法
- 不用加减乘除实现加法
- SEM of C language_ Tvariable type
- C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display
- 4、【WebGIS实战】软件操作篇——数据导入及处理
- 使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况
- The shell script uses two bars to receive external parameters
- 4. [WebGIS practice] software operation chapter - data import and processing
猜你喜欢

TEC: Knowledge Graph Embedding with Triple Context

Binary tree god level traversal: Morris traversal

E15 solution for cx5120 controlling Huichuan is620n servo error
![5. [WebGIS practice] software operation - service release and permission management](/img/5d/070e207bd96e60ba1846d644d4fb54.png)
5. [WebGIS practice] software operation - service release and permission management

实现pow(x,n)函数

TEC: Knowledge Graph Embedding with Triple Context

Complete knapsack problem

5、【WebGIS实战】软件操作篇——服务发布及权限管理

Implement pow (x, n) function

How to achieve 0 error (s) and 0 warning (s) in keil5
随机推荐
静态库使用MFC和共享库使用MFC的区别
Leetcode:829. Sum of continuous integers
FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
Leetcode 31 next spread, leetcode 64 minimum path sum, leetcode 62 different paths, leetcode 78 subset, leetcode 33 search rotation sort array (modify dichotomy)
数据交换 JSON
深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
Valid brackets (force deduction 20)
访问阿里云存储的图片URL实现在网页直接预览略缩图而不直接下载
The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
The difference between MFC for static libraries and MFC for shared libraries
Download and installation configuration of cygwin
监听器 Listener
How to use hybrid format to output ISO files? isohybrid:command not found
Appium fundamentals of automated testing - basic principles of appium
Design of serial port receiving data scheme
Leetcode 1818 absolute value, sorting, dichotomy, maximum value
Processing of menu buttons on the left and contents on the right of the background system page, and double scrolling appears on the background system page
C语言的sem_t变量类型
ECMAScript 6.0