(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 ...
随机推荐
- 「THUWC 2017」随机二分图
「THUWC 2017」随机二分图 解题思路 : 首先有一个 \(40pts\) 的做法: 前 \(20pts\) 暴力枚举最终的匹配是怎样的,check一下计算方案数,后 \(20pts\) 令 \ ...
- BZOJ2157: 旅游 树链剖分 线段树
http://www.lydsy.com/JudgeOnline/problem.php?id=2157 在对树中数据进行改动的时候需要很多pushdown(具体操作见代码),不然会wa,大概原因 ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
- Java Queue的测试
上传图片没上去,提交的时候已经结束 代码链接
- PHP链接sqlserver出现中文乱码
PHP通过dblib扩展链接sqlserver,使用的是freetds,出现中文乱码. 在freetds的配置文件中(/usr/local/freetds/etc/freetds.conf),[glo ...
- day78_淘淘商城项目_11_单点登录系统实现 + 用户名回显 + ajax请求跨域问题详解_匠心笔记
课程计划 1.SSO注册功能实现 2.SSO登录功能实现 3.通过token获得用户信息 4.ajax跨域请求解决方案--jsonp 1.服务接口实现 SSO系统就是解决分布式环境下登录问题的,本 ...
- [转] Android 命名规范 (提高代码可以读性)
Android命名规范编码习惯 刚接触android的时候,命名都是按照拼音来,所以有的时候想看懂命名的那个控件什么是什么用的,就要读一遍甚至好几遍才知道,这样的话,在代码的 审查和修改过程中就会浪费 ...
- Codeforces Round #194 (Div. 1) B. Chips 水题
B. Chips Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/333/problem/B D ...
- ASP.NET中在一般处理程序中使用session的简单介绍
这篇文章介绍了ASP.NET中在一般处理程序中使用session,有需要的朋友可以参考一下 <%@ WebHandler Language="C#" Class=" ...
- 支持Tasker控制的app合集
跟各种Tasker插件打交道,原因有两点: 1.站在开发者的角度:Tasker虽为神器,也不能面面俱到,一个原因就是Android自身过于分裂化造成的,不可能兼顾全平台和机型:个人开发者精力有限,也满 ...