原题链接在这里: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. Java:执行jar文件命令

    Java:执行jar文件命令 执行jar文件命令: java -jar test.jar win7系统切换目录命令: cd /d d:/test

  2. linux根分区满了如何处理,查找大文件方法

    一:如果linux根分区使用量达到100%,会造成如下现象: root不能登录 系统不能正常启动 二:通过命令查找根分区内的大文件 du -sh /* 2>/dev/null | sort -h ...

  3. git原理学习

    http://git.oschina.net/progit/   这一点值得牢记:Git 会把工作目录的内容恢复为检出某分支时它所指向的那个提交对象的快照.它会自动添加.删除和修改文件以确保目录的内容 ...

  4. RemoveDuplicatesFromSortedArrayI II,移除有序数组里的重复元素以及移除数组里的某个元素

    RemoveDuplicatesFromSortedArrayI: 问题描述:给定一个有序数组,去掉其中重复的元素.并返回新数组的长度.不能使用新的空间. [1,1,2,3] -> [1,2,3 ...

  5. Matlab操作矩阵的相关方法

    Matlab操作矩阵的相关方法 下面这篇文章主要是对吴恩达老师机器学习中matlab操作的一个整理和归纳 一.基本操作 1.生成矩阵(ones.zeros) A = [1 2;3 4;5 6]    ...

  6. TextView实现图文混合编排

    TextView实现图文混合编排 一.简介 在这里实现图文混合编排使用的是:TextView中预定义的类似Html的标签 二.方法 * 1.设置好html标签的文本 String html=" ...

  7. MySQL二进制日志功能介绍

    二进制日志记录所有更新数据的SQL语句,其中也包含可能更新数据的SQL语句,例如DELETE语句执行过程中无匹配的行.二进制日志中还包含了与执行SQL语句相关的内容,例如SQL语句执行的时间.错误代码 ...

  8. Python — List、Set、Tuple、Dictionary之间的区别、参数传递

    1.list 列表 有序集合,随时增删.包含的数据类型可以不同:整数.浮点数.字符串.list.tuple.dict.set.bool.空值.常量. list = [12, 'Yummy', 19.2 ...

  9. maven 其他远程仓库配置

    在平时的开发中,我们往往不会使用默认的中央仓库,默认的中央仓库访问的速度比较慢,访问的人或许很多,有时候也无法满足我们项目的需求,可能项目需要的某些构件中央仓库中是没有的,而在其他远程仓库中有,如JB ...

  10. tsunami:一种基于UDP协议的快速传输

    一. 需求 最近在做数据库迁移,经常需要打包实例传输,传统scp感觉很慢. 二. 软件信息 1. 软件主页:http://tsunami-udp.sf.net/ 2. 软件安装:直接源码make &a ...