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. ...
随机推荐
- github Git-fork-别人的项目后更新代码的方法
用github还处于菜的阶段,遇到问题简单记录. 举个例子,需要 fork 这个项目 https://github.com/tarobjtu/WebFundamentals.git 点击 for ...
- vue切换路由模式{hash/history}
vue中常用的路由模式 hash(#):默认路由模式 histroy(/)切换路由模式 切换路由模式 export default new Router({ // 路由模式:hash(默认),hist ...
- invalid derived query的解决办法
标签: eclipse / invalid / derived / 解决办法 / 校验功能 479 在Eclipse的运行过程中,突然有一个接口跳出如下错误: invalid derived quer ...
- [BZOJ1587]叶子合并leaves
Description 在一个美丽的秋天,丽丽每天都经过的花园小巷落满了树叶,她决定把树叶堆成K堆,小巷是笔直的 共有N片树叶(树叶排列也是笔直的),每片树叶都有一个重量值,并且每两片想邻的树叶之间的 ...
- [BZOJ1823]满汉全席
Description 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的材料透过满族或是汉族的料理方式,呈现在數量繁多的菜色之中.由于菜色众多而繁杂,只有极少數博学多闻技艺高超的厨师能够做出满汉全席,而 ...
- 调用webservices中 枚举类型没有被序列化问题
引用服务后,代理类为自动为所有枚举类型生成了一个Bool类型相关字段,命名方式:比如枚举类名为“PayType”,生成的相关字段为“PayTypeSpecified”,此字段有何作用? PayType ...
- JVM 深入浅出
jvm 相信大家都有认知 在面试中也是经常遇见的希望接下来的这篇文章可以对你们带来帮助 java 的好处 : java这门语言之所以受广大的码农所喜爱是因为它不像C语言需要程序员自己手动分配内存空间 ...
- Pandas面板(Panel)
面板(Panel)是3D容器的数据.面板数据一词来源于计量经济学,部分源于名称:Pandas - pan(el)-da(ta)-s. 3轴(axis)这个名称旨在给出描述涉及面板数据的操作的一些语义. ...
- DESUtil 加密算法
import java.security.Key; import java.security.Security; import javax.crypto.Cipher; /** *@Title: DE ...
- sql server数据库课程设计分析
课题:能源管理收费系统 系统功能的基本要求: (1)用户基本信息的录入:包括用户的单位.部门.姓名.联系电话.住址 : (2)用户水.电.气数据的录入(每个月的数据的录入): (3)水.电.气价格的管 ...