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的更多相关文章

  1. LeetCode(72) Edit Distance

    题目 Given two words word1 and word2, find the minimum number of steps required to convert word1 to wo ...

  2. [Leetcode 72]编辑距离 Edit Distance

    [题目] Given two words word1 and word2, find the minimum number of operations required to convert word ...

  3. 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 ...

  4. (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 ...

  5. ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java

    Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...

  6. [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 ...

  7. [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 ...

  8. [LeetCode#161] One Edit Distance

    Problem: Given two strings S and T, determine if they are both one edit distance apart. General Anal ...

  9. (LeetCode 78)SubSets

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

随机推荐

  1. Spring boot的hot swapping

    前言 嘛,都是看官方文档的,就先贴上文档地址: using-boot-hot-swapping 使用 使用hot swapping只需要把devtools的jar包添加到你的classpath里. 在 ...

  2. java 错误:无法找到或装入主类

    1. 删除找不到的jar 2. 删除src以外的文件夹

  3. Visitor设计模式

    我猜想许多人都知道访问者设计模式,这种模式在“四人帮”的那本可复用面向对象软件基础的书被描述过.这个模式自身其实一点也不复杂(和以往的其他设计模式一样).  如上图所示: 我知道这个模式很久了,但是我 ...

  4. OpenVAS漏洞扫描基础教程之创建用户组与创建角色

    OpenVAS漏洞扫描基础教程之创建用户组与创建角色 OpenVAS创建用户组 用户组就是指许多个用户的组合.在网络中,各个访问网络的用户的权限可能各不相同.所以,可以通过将具体相同权限的用户划为一组 ...

  5. 【BZOJ 3090】 树形DP

    3090: Coci2009 [podjela] Description 有 N 个农民, 他们住在 N 个不同的村子里. 这 N 个村子形成一棵树.每个农民初始时获得 X 的钱.每一次操作, 一个农 ...

  6. bzoj4144 [AMPPZ2014]Petrol

    link 题意: 给一个n个点m条边的带权无向图,其中k个点是加油站,每个加油站可以加满油,但不能超过车的油量上限.有q个询问,每次给出x,y,b,保证x,y都是加油站,问一辆油量上限为b的车从x出发 ...

  7. Android UI设计规范之知识点

    界面尺寸 android的尺寸众多,建议使用分辨率为720×1280的尺寸设计.这个尺寸720×1280中显示完美,在1080×1920中看起来也比较清晰;切图后的图片文件大小也适中,应用的内存消耗也 ...

  8. JSON 与 JS 对象的关系

    很多人搞不清楚 JSON 和 Js 对象的关系,甚至连谁是谁都不清楚.简单来说: JSON 是 JS 对象的字符串表示法,它使用文本表示一个 JS 对象的信息,本质是一个字符串. 如 var obj ...

  9. 01-项目简介Springboot简介入门配置项目准备

    总体课程主要分为4个阶段课程: ------------------------课程介绍------------------------ 01-项目简介Springboot简介入门配置项目准备02-M ...

  10. linux基础命令学习(四)用户与群组

    一.linux用户账号的管理 linux用户账号的管理主要包括用户添加.用户删除.用户修改. 添加用户账号就是在系统创建一个新账号,然后为新账号分为用户号.用户组.主目录和登录Shell等资源. 刚添 ...