原题链接在这里:https://leetcode.com/problems/delete-operation-for-two-strings/description/

题目:

Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.

Example 1:

Input: "sea", "eat"
Output: 2
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".

Note:

  1. The length of given words won't exceed 500.
  2. Characters in given words can only be lower-case letters.

题解:

找LCS的长度n. word1.length()+word2.length()-2*n. 就是答案.

用DP找LCS的长度. 需要储存的历史信息是到当前点的LCS长度. 用dp[i][j]储存, 表示word1到i和word2到j的LCS长度.

递推时, 若是当前字符match, 在dp[i-1][j-1]的基础上加1即可.

若不match, 取dp[i][j-1] 和 dp[i-1][j]中较大值即可.

初始化都是0.

Time Complexity: O(m*n). m = word1.length(), n = word2.length().

Space: O(m*n).

AC Java:

 class Solution {
public int minDistance(String word1, String word2) {
int len1 = word1.length();
int len2 = word2.length();
int [][] dp = new int[len1+1][len2+1];
for(int i = 0; i<=len1; i++){
for(int j = 0; j<=len2; j++){
if(i==0 || j==0){
continue;
}else if(word1.charAt(i-1) == word2.charAt(j-1)){
dp[i][j] = 1 + dp[i-1][j-1];
}else{
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
}
}
}
return len1+len2-2*dp[len1][len2];
}
}

或者像Edit Distance直接计算需要最小的operations数目.

DP问题. 需要储存的历史信息是各自到当前的位置变成相同string需要的最小operation数目. 用二维数组来出巡.

递推时, 若是当前字符match, 不需要额外操作. dp[i][j] = dp[i-1][j-1].

若不match, 需要在dp[i-1][j], dp[i][j-1]中取较小值加1.

初始化或一边在初始位置没动, 最小operation数目就是另一边的位置全部减掉.

答案dp[m][n]. m = word1.length(). n = word2.length().

Time Complexity: O(m*n).

Space: O(m*n).

AC Java:

 class Solution {
public int minDistance(String word1, String word2) {
int m = word1.length();
int n = word2.length();
int [][] dp = new int[m+1][n+1];
for(int i = 0; i<=m; i++){
for(int j = 0; j<=n; j++){
if(i == 0 || j == 0){
dp[i][j] = i+j;
}else if(word1.charAt(i-1) == word2.charAt(j-1)){
dp[i][j] = dp[i-1][j-1];
}else{
dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1]) + 1;
}
}
}
return dp[m][n];
}
}

也可以降维节省空间.

Time Complexity: O(m*n).

Space: O(n).

AC Java:

 class Solution {
public int minDistance(String word1, String word2) {
int m = word1.length();
int n = word2.length();
int [] dp = new int[n+1];
for(int i = 0; i<=m; i++){
int [] temp = new int[n+1];
for(int j = 0; j<=n; j++){
if(i == 0 || j == 0){
temp[j] = i+j;
}else if(word1.charAt(i-1) == word2.charAt(j-1)){
temp[j] = dp[j-1];
}else{
temp[j] = Math.min(dp[j], temp[j-1]) + 1;
}
}
dp = temp;
}
return dp[n];
}
}

类似Longest Common SubsequenceMinimum ASCII Delete Sum for Two StringsEdit Distance.

LeetCode Delete Operation for Two Strings的更多相关文章

  1. [LeetCode] Delete Operation for Two Strings 两个字符串的删除操作

    Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...

  2. 【Leetcode】583. Delete Operation for Two Strings

    583. Delete Operation for Two Strings Given two words word1 and word2, find the minimum number of st ...

  3. LC 583. Delete Operation for Two Strings

    Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...

  4. [LeetCode] 583. Delete Operation for Two Strings 两个字符串的删除操作

    Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...

  5. 【LeetCode】583. Delete Operation for Two Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. LeetCode 583 Delete Operation for Two Strings 删除两个字符串的不同部分使两个字符串相同,求删除的步数

    Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...

  7. 583. Delete Operation for Two Strings

    Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...

  8. [Swift]LeetCode583. 两个字符串的删除操作 | Delete Operation for Two Strings

    Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...

  9. [LeetCode] Minimum ASCII Delete Sum for Two Strings 两个字符串的最小ASCII删除和

    Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...

随机推荐

  1. iOS 那些年我们遇到的坑

    1坑: UITableView的第一个Cell下移

  2. springboot创建多环境profile打包

    springboot开发打包时,一般会有多个环境,dev,qa,prod等,配置文件大多雷同,只是方便开发切换,但是生成部署时产生的war包就无需这么多重复配置了,这时这些dev,qa的配置就不应该打 ...

  3. 内核hlist的使用

    struct hlist_head { struct hlist_node *first; }; struct hlist_node { struct hlist_node *next, **ppre ...

  4. (转)Java编译后产生class文件的命名规则

      今天刚好有同学问了下Java编译后产生的.class文件名的问题,虽然一直都在使用Java做开发,但是之前对编译后产生的.class文件名的规范也基本没做了解过,也真的是忏愧啊!今天无论如何都要总 ...

  5. MSSQL2005数据库显示单一用户模式,无法进行任何操作

    MSSQL2005数据库显示单一用户模式,无法进行任何操作 经查询,使用exec sp_who进行查看链接线程,发现仍然有链接不断进行请求,将链接踢出,然后通过命令修复即可恢复 处理步骤: exec ...

  6. MailJobUtils 发送邮件

    import java.util.List; import java.util.Properties; import javax.annotation.Resource; import javax.m ...

  7. mysql查询结果带上序号

    select (@i:=@i+1) as rownum,t1.id ","from mega_user t1,(select @i:=0) t2 order by t1.gold ...

  8. 【scala】应用程序和App特性

    一.应用程序 要运行一个Scala对象,必须提供一个独立对象的名称.这个独立对象需要包含一个main方法,该方法接受一个Array[String]作为参数,结果类型为Unit. import Chec ...

  9. LeetCode OJ:Longest Common Prefix(最长公共前缀)

    Write a function to find the longest common prefix string amongst an array of strings. 求很多string的公共前 ...

  10. react: menuService

    1.获取菜单对象 static findCurrentItem(items, currentState, currentItem) { _.forEach(items, function (item) ...