(LeetCode 72)Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
You have the following 3 operations permitted on a word:
a) Insert a character
b) Delete a character
c) Replace a character
题目:
给定两个字符串,求把S变成T所需的最小操作数。
3种字符操作分别为插入、删除、替换。
思路:
动态规划思想:
假设dp[i][j]表示以S[i]结尾的字符串和以T[j]结尾的字符串转换所需的最小操作数,考虑三种操作,然后取三者最小值:
1、替换:
假设S[i-1],T[j-1]已对齐,即dp[i-1][j-1]已知,则当S[i]==T[j]时,dp[i][j]=dp[i-1][j-1],否则,dp[i][j]=dp[i-1][j-1]+1.
2、删除
假设S[i-1],T[j]已对齐,即dp[i-1][j]已知,多出来的S[i]需删除,操作数+1,则dp[i][j]=dp[i-1][j]+1.
3、插入
假设S[i],T[j-1]已对齐,即dp[i][j-1]已知,需在S中插入S[i+1]=T[j]来匹配,操作数+1,则dp[i][j]=dp[i][j-1]+1.
状态转移方程:
dp[i][j]=min(dp[i-1][j-1]+(S[i]==T[j]?0,1),dp[i-1][j]+1,dp[i][j-1]+1)
初始值:
dp[i][0]=i
dp[0][j]=j
复杂度:
时间复杂度:O(m*n)
空间复杂度:O(m*n)
空间优化:
由状态转移方程可知,dp[i][j]与dp[i-1][j-1],dp[i-1][j],dp[i][j-1]有关,可以去掉一维,只留下dp[j]。
等式右边的dp[i-1][j]和dp[i][j-1]都可以直接改成dp[j](旧的值)和dp[j-1](已更新),只有dp[i-1][j-1]没有记录下来,通过某个变量保存起来之后就可以。
因此空间复杂度:O(n)
代码:
class Solution {
public:
int minDistance(string word1, string word2) {
int m=word1.length();
int n=word2.length();
vector<vector<int> > distance(m+1,vector<int>(n+1));
for(int i=0;i<=m;i++){
for(int j=0;j<=n;j++){
if(0==i){
distance[i][j]=j;
}
else if(0==j){
distance[i][j]=i;
}
else{
distance[i][j]=min(distance[i-1][j-1]+((word1[i-1]==word2[j-1])?0:1),
min(distance[i-1][j]+1,distance[i][j-1]+1)
);
}
}
}
return distance[m][n];
}
};
class Solution {
public:
int minDistance(string word1, string word2) {
int m=word1.length();
int n=word2.length();
vector<int> distance(n+1);
for(int i=0;i<=m;i++){
int last;
for(int j=0;j<=n;j++){
if(0==i){
distance[j]=j;
}
else if(0==j){
last=distance[j];
distance[j]=i;
}
else{
int temp=distance[j];
distance[j]=min(last+((word1[i-1]==word2[j-1])?0:1),
min(distance[j]+1,distance[j-1]+1)
);
last=temp;
}
}
}
return distance[n];
}
};
(LeetCode 72)Edit Distance的更多相关文章
- LeetCode(72) Edit Distance
题目 Given two words word1 and word2, find the minimum number of steps required to convert word1 to wo ...
- [Leetcode 72]编辑距离 Edit Distance
[题目] Given two words word1 and word2, find the minimum number of operations required to convert word ...
- leetcode@ [72/115] Edit Distance & Distinct Subsequences (Dynamic Programming)
https://leetcode.com/problems/edit-distance/ Given two words word1 and word2, find the minimum numbe ...
- (Problem 72)Counting fractions
Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...
- ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java
Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...
- [leetcode]161. One Edit Distance编辑步数为一
Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...
- [LeetCode] 161. One Edit Distance 一个编辑距离
Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...
- [LeetCode#161] One Edit Distance
Problem: Given two strings S and T, determine if they are both one edit distance apart. General Anal ...
- (LeetCode 78)SubSets
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
随机推荐
- 为什么我喜欢Java
我现在的老板使用一个在线测试系统来筛选在线申请职位的求职者.测试的第一个问题很浅显,仅仅是为了让求职者熟悉一下这个系统的提交和测试代码的流程.问题是这样的,写一个将标准输入拷贝到标准输出的流程.求职者 ...
- Google的代码高亮-code-prettify
前不久发现,在wordpress中贴代码的时候,发现code标签并没有意料中的好使用,在贴代码的时候没有高亮真的是一件无法忍受的事情. 正巧,两周前听过同事Eason的一个关于Markdown的分享, ...
- luogu P4137 mex
题面: 有一个长度为$n$的数组${a1,a2,…,an}$.$m$次询问,每次询问一个区间内最小没有出现过的自然数. 令$lst[i][r]$表示在$[1, r]$中数值$i$最后出现的位置 那么, ...
- POJ 3974 Palindrome 字符串 Manacher算法
http://poj.org/problem?id=3974 模板题,Manacher算法主要利用了已匹配回文串的对称性,对前面已匹配的回文串进行利用,使时间复杂度从O(n^2)变为O(n). htt ...
- 使用百度ai接口加图灵机器人完成简单web版语音对话
app文件 from flask import Flask, request, render_template, jsonify, send_file from uuid import uuid4 i ...
- ASP.Net如何用Cookies保存对象
在ASP.Net中,有时候考虑到较多的使用Session来保存对象,会增加服务器的负载,所以我们会选择用Cookies来保存对象的状态,而Cookies只能保存字符串,这时,我们可以考虑用序列化操作来 ...
- PSCollectionView瀑布流实现
[-] 一基本原理 二具体实现 相关数据结构 视图更新方式 relayoutViews方法 removeAndAddCellsIfNecessary方法 select方法 重用数据块视图机制 三使用方 ...
- Restrict form resize -- Delphi
http://www.delphipages.com/forum/showthread.php?t=58391 Hi, How would I restrict a form from being r ...
- Windows 配置 Apache Python CGI
提示:安装Apache可参考 https://jingyan.baidu.com/article/0eb457e53c019f03f1a905c7.html 1. 打开URL: https://ww ...
- Spring Data JPA -1-CRUD入门
1) 引入jar包支持 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...