leetcode第72题:编辑距离
给定两个单词 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'
网易面试时遇到了这题,当时没做出来,想不出来状态转移方程。
解题思路如下:
首先定义状态矩阵,dp[m][n],其中m为word1的长度+1,n为word2的长度+1,为什么+1?因为要考虑如果word1或word2为空的情况,后面可以看到。
定义dp[i][j]为word1中前i个字符组成的串,与word2中前j个字符组成的串的编辑距离。
插入操作:在word1的前i个字符后插入一个字符,使得插入的字符等于新加入的word2[j]。这里要考虑清楚,插入操作对于原word1字符来说,i是没有前进的,而对于word2来说是前进了一位然后两个字符串才相等的。所以此时是dp[i][j]=dp[i][j-1]+1。
删除操作:在word1的第i−1个字符后删除一个字符,使得删除后的字符串word[:i-1]与word2[:j]相同。这里要考虑清楚,删除操作对于原word2字符来说,j−1是没有前进的,而对于word1来说是删除了一位然后两个字符串才相等的。所以此时是dp[i][j]=dp[i-1][j]+(0 or 1)。

代码如下:
class Solution:
def minDistance(self, word1, word2):
m=len(word1)+1; n=len(word2)+1
dp = [[0 for i in range(n)] for j in range(m)]
for i in range(n):
dp[0][i]=i
for i in range(m):
dp[i][0]=i
for i in range(1,m):
for j in range(1,n):
if word1[i-1] == word2[j-1]:
dp[i][j] = dp[i-1][j-1]
else:
dp[i][j] = min(dp[i][j-1], dp[i-1][j], dp[i-1][j-1]) + 1
return dp[m-1][n-1]
word1 = "intention"
word2 = "execution"
test = Solution()
print(test.minDistance(word1, word2))
答案来源:https://blog.csdn.net/iyuanshuo/article/details/80112211
def minDistance(self, S1, S2):
m = len(S1) + 1;
n = len(S2) + 1
dp = [[0 for i in range(n)] for j in range(m)]
for i in range(n):
dp[0][i] = i
for i in range(m):
dp[i][0] = i
for i in range(1, m):
for j in range(1, n):
if S1[i - 1] == S2[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1
return dp[m - 1][n - 1]
leetcode第72题:编辑距离的更多相关文章
- Leetcode OJ 刷题
Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...
- [LeetCode] 系统刷题5_Dynamic Programming
Dynamic Programming 实际上是[LeetCode] 系统刷题4_Binary Tree & Divide and Conquer的基础上,加上记忆化的过程.就是说,如果这个题 ...
- leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- leetcode第37题--Count and Say
题目:(据说是facebook的面试题哦) The count-and-say sequence is the sequence of integers beginning as follows:1, ...
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode的刷题利器(伪装到老板都无法diss你没有工作)
在工程效率大行其道的今天,如果不会写点代码以后也不容易在测试圈混下去.今天给大家推荐一个LeetCode的刷题利器,可以伪装到连你老板在这里走过去都无法确认你是在干活呢,还是在干活呢. LeetCod ...
- LeetCode每天一题之两数之和
这个LeetCode刷题系列的博客权当是为自己记一下笔记吧.博客系列会从LeetCode的第一题开始刷,同时会从零开始学习[因为我就是零/(ㄒoㄒ)/~~].同时,如果有写错的地方,希望大佬们在评论区 ...
- leetcode第三题
leetcode第三题: 题目: 给定一个字符串,找出不含有重复字符的最长子串的长度. 源码(使用java语言): class Solution { public int lengthOfLonges ...
随机推荐
- python记录_day018 md5加密
MD5 用法: import hashlib obj = hashlib.md5(加盐) obj.update(明文的bytes) obj.hexdigest() 获取密文 示例: import ha ...
- 『MXNet』第一弹_基础架构及API
MXNet是基础,Gluon是封装,两者犹如TensorFlow和Keras,不过得益于动态图机制,两者交互比TensorFlow和Keras要方便得多,其基础操作和pytorch极为相似,但是方便不 ...
- 关于react16.4——错误边界
过去,组件内的 JavaScript 错误常常会破坏 React 内部状态,并导致它在下一次渲染时产生神秘的错误.这些错误总会在应用代码中较早的错误引发的,但 React 并没有提供一种方式能够在组件 ...
- leetcode-algorithms-18 4Sum
leetcode-algorithms-18 4Sum Given an array nums of n integers and an integer target, are there eleme ...
- Mysql优化系列--Innodb引擎下mysql自身配置优化-转
原文链接:http://www.cnblogs.com/kevingrace/p/6133818.html 谢谢楼主 1.简单介绍 InnoDB给MySQL提供了具有提交,回滚和崩溃恢复能力的事务安全 ...
- 如何引用GitHub的静态资源文件 js css
参考:引用GitHub的静态资源文件 有些人说直接用 Github Raw 浏览器不执行是因为返回的 content-type 是 text/plain,这么说不准确.实际上浏览器对 MIME 类型并 ...
- python爬虫---BeautifulSoup的用法
BeautifulSoup是一个灵活的网页解析库,不需要编写正则表达式即可提取有效信息. 推荐使用lxml作为解析器,因为效率更高. 在Python2.7.3之前的版本和Python3中3.2.2之前 ...
- Intersecting Lines
Intersecting Lines We all know that a pair of distinct points on a plane defines a line and that a p ...
- vue 中router.go、router.push和router.replace的区别
router.go(n) 这个方法的参数是一个整数,意思是在 history 记录中向前或者后退多少步,类似 window.history.go(n) router.push(location) 想要 ...
- js 日期格式: UTC GMT 互相转换
//UTC 转指定格式日期 let date = '2018-03-07T16:00:00.000Z' console.log(moment(date).format('YYYY-MM-DD HH:m ...