当前位置:网站首页>72. edit distance
72. edit distance
2022-07-01 03:43:00 【Sun_ Sky_ Sea】
72. Edit distance
Original title link :https://leetcode.cn/problems/edit-distance/
Here are two words for you word1 and word2, Please return to word1 convert to word2 The minimum number of operands used .
You can do the following three operations on a word :
Insert a character
Delete a character
Replace a character
Example 1:
Input :word1 = “horse”, word2 = “ros”
Output :3
explain :
horse -> rorse ( take ‘h’ Replace with ‘r’)
rorse -> rose ( Delete ‘r’)
rose -> ros ( Delete ‘e’)
Example 2:
Input :word1 = “intention”, word2 = “execution”
Output :5
explain :
intention -> inention ( Delete ‘t’)
inention -> enention ( take ‘i’ Replace with ‘e’)
enention -> exention ( take ‘n’ Replace with ‘x’)
exention -> exection ( take ‘n’ Replace with ‘c’)
exection -> execution ( Insert ‘u’)
Tips :
0 <= word1.length, word2.length <= 500
word1 and word2 It's made up of lowercase letters
Their thinking :
Definition dp[i][j] Means :word1 Before i Characters and word2 Before j Edit distance of characters . Namely word1 Before i Characters , become word2 Before j Characters , It takes at least so many steps . If one of the strings is empty , Then the edit distance is the length of another string ; When both strings are not empty ,dp[i][j]=min(dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+int(word1[i]!=word2[j])):
for example ,word1 = “abcde”, word2 =“fgh”, Now let's calculate the editing distance between these two strings , Is to find from word1, At least how many steps , Can become word2? There are three ways :know "abcd" become "fgh" How many steps ( hypothesis X Step ), So from "abcde" To "fgh" Namely "abcde"->“abcd”->“fgh”.( Delete... At a time , Add X Step , in total X+1 Step )
know "abcde" become “fg” How many steps ( hypothesis Y Step ), So from "abcde" To "fgh" Namely "abcde"->“fg”->“fgh”.( First Y Step , Add again , Add X Step , in total Y+1 Step )
know "abcd" become “fg” How many steps ( hypothesis Z Step ), So from "abcde" To "fgh" Namely "abcde"->“fge”->“fgh”( Replace ).
Code implementation :
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
m, n = len(word1), len(word2)
if m * n == 0:
return m + n
# Definition dp[i][j]:word1 To i The characters of Transform into word2[j] Editor's distance
dp = [[0] * (n + 1) for _ in range(m + 1)]
# initialization dp:word1 Empty or word2 It's empty , Then the minimum number of steps of conversion
# Namely word1 or word2 The length of
for i in range(m + 1):
dp[i][0] = i
for j in range(n + 1):
dp[0][j] = j
# according to dp[i - 1][j] dp[i][j - 1] dp[i - 1][j - 1]
# Calculation dp[i][j] Value
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]
reference :
https://leetcode.cn/problems/edit-distance/solution/bian-ji-ju-chi-by-leetcode-solution/
边栏推荐
- Research on target recognition and tracking based on 3D laser point cloud
- Implement pow (x, n) function
- 392. 判断子序列
- Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)
- Home online shopping project
- 【TA-霜狼_may-《百人計劃》】2.3 常用函數介紹
- How keil displays Chinese annotations (simple with pictures)
- Develop industrial Internet with the technical advantages of small programs
- 【EI会议】2022年国际土木与海洋工程联合会议(JCCME 2022)
- Thread data sharing and security -threadlocal
猜你喜欢
Use of comment keyword in database
IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?
衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)
Cookie&Session
【TA-霜狼_may-《百人計劃》】2.3 常用函數介紹
idea插件备份表
C语言的sem_t变量类型
Addition without addition, subtraction, multiplication and division
Explain spark operation mode in detail (local+standalone+yarn)
不用加减乘除实现加法
随机推荐
【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
Cookie&Session
TEC: Knowledge Graph Embedding with Triple Context
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
数据库中COMMENT关键字的使用
数据库DDL(Data Definition Language,数据定义语言)知识点
二叉树神级遍历:Morris遍历
ASGNet论文和代码解读2
Error: plug ins declaring extensions or extension points must set the singleton directive to true
Jeecgboot output log, how to use @slf4j
后台系统右边内容如何出现滚动条和解决双滚动条的问题
还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板
【伸手党福利】JSONObject转String保留空字段
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
Sort linked list (merge sort)
Home online shopping project
【EI会议】2022年第三届纳米材料与纳米技术国际会议(NanoMT 2022)
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
[TA frost wolf _may - "hundred people plan"] 1.4 introduction to PC mobile phone graphics API