LeetCode Delete Operation for Two Strings
原题链接在这里: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:
- The length of given words won't exceed 500.
- 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 Subsequence, Minimum ASCII Delete Sum for Two Strings, Edit Distance.
LeetCode Delete Operation for Two Strings的更多相关文章
- [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 ...
- 【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 ...
- 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 ...
- [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 ...
- 【LeetCode】583. Delete Operation for Two Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- 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 ...
- [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 ...
- [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. ...
随机推荐
- 微服务与SOA
微服务跟SOA有什么区别呢,可以把微服务当做去除了ESB的SOA.ESB是SOA架构中的中心总线,拓扑结构应该是星形的,而微服务是去中心化的分布式软件架构. 一.巨石(monolith) web应用程 ...
- 饭卡管理系统学生E-R图
- 2 - 1.1 可行性分析 1.1.1 项目背景 近年来学生食堂饭卡的使用给高校餐饮管理带来了一次革命, 从结算方式到账户管理, 从卫生便捷到数据统计等,不仅给就餐者带来了方便,也使餐饮结算手段发 ...
- Linux系统下chkconfig命令使用详解
chkconfig命令可以用来检查.设置系统的各种服务 使用语法:chkconfig [--add][--del][--list][系统服务] 或 chkconfig [--level <等级代 ...
- Linux内核模块编写详解
内核编程常常看起来像是黑魔法,而在亚瑟 C 克拉克的眼中,它八成就是了.Linux内核和它的用户空间是大不相同的:抛开漫不经心,你必须小心翼翼,因为你编程中的一个bug就会影响到整个系统,本文给大家介 ...
- IOS 出现错误 :Reason: image not found
把Build Phases 里HyphenateLite.framework后边的选项修改成为Optional就可以了 dyld 后面有提示 HyphenateLite.framework
- 如何理解Hibernate的延迟加载机制?在实际应用中,延迟加载与Session关闭的矛盾是如何处理的?
延迟加载就是并不是在读取的时候就把数据加载进来,而是等到使用时再加载.Hibernate使用了虚拟代理机制实现延迟加载,我们使用Session的load()方法加载数据或者一对多关联映射在使用延迟加载 ...
- windchill系统——开发_客户端自定义
步骤如下
- Bellman-Ford算法优化
2017-07-27 16:02:48 writer:pprp 在BEllman-Ford算法中,其最外层的循环的迭代次数为n-1,如果不存在负权回路,需要迭代的次数是远远小于n-1; 如果在某一次迭 ...
- 用户iis可以用外网ip访问,用内网访问报错404
如下,没有添加内网ip绑定
- C++ dll的隐式与显式调用
应用程序使用DLL可以采用两种方式:一种是隐式链接,另一种是显式链接.在使用DLL之前首先要知道DLL中函数的结构信息.Visual C++6.0(或者更先进的版本)在VC\bin目录下提供了一个名为 ...