(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 ...
随机推荐
- 【栈模拟dfs】Cells UVALive - 3486
题目链接:https://cn.vjudge.net/contest/209473#problem/D 题目大意:有一棵树,这棵树的前n个节点拥有子节点,告诉你n的大小,以及这n个节点各有的子节点个数 ...
- Linux命令之gdisk
gdisk -l [设备] gdisk又叫GPT fdisk,算是fdisk的延伸吧,主要使用的是GPT分区类型,用来划分容量大于2T的硬盘. 扩展1:分区类型GPT和MBR.GPT最大支持18EB( ...
- 更改paramiko 源码 记录命令实现堡垒机功能
利用paramiko 下的demo可以很容易的实现记录客户在操作客户机时的命令,修改\demos\interactive.py def posix_shell(chan): import select ...
- 理解面向消息中间件及JMS 以及 ActiveMQ例子
为了帮助你理解ActiveMQ的意义,了解企业消息传送背景和历史是很重要的.讨论完企业消息传送,你将可以通过一个小例子了解JMS及其使用.这章的目的是简要回顾企业消息传送及JMS规范.如果你已经熟悉这 ...
- android remoteView
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha remoteView 可以在 appWidget 和 notification 中 使 ...
- 网络流24题之最长k可重区间集问题
对于每个点向后一个点连流量为k费用为0的边 对每一区间连l到r流量为1费用为r-l的边 然后最小费用最大流,输出取反 一开始写的r-l+1错了半天... By:大奕哥 #include<bits ...
- bzoj 1143: [CTSC2008]祭祀river / 2718: [Violet 4]毕业旅行 -- 二分图匹配
1143: [CTSC2008]祭祀river Time Limit: 10 Sec Memory Limit: 162 MB Description 在遥远的东方,有一个神秘的民族,自称Y族.他们 ...
- WebSQL的基本使用过程
1.创建或打开数据库(openDatabase) var db = openDatabase('dbname', '1.0', 'discription', 2 * 1024); // 目前测试只有C ...
- 密码加密_md5
md5加密 package com.fh.util; import java.security.MessageDigest; public class MD5 { public static Stri ...
- Educational Codeforces Round 12 F. Four Divisors 求小于x的素数个数(待解决)
F. Four Divisors 题目连接: http://www.codeforces.com/contest/665/problem/F Description If an integer a i ...