[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 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.
求出最长相同子序列Longest Common Subsequence,然后两个单词长度和减去2倍的相同子序列长度就是答案。
解法1: 递归, 如果[0, i], [0, j]最后一个字符相同,则比较[0, i-1], [0, j-1]的最后一个字符,若不相同,则删去第i个或第j个字符后,返回长度更长的子序列。TLE
解法2:动态规划dp,dp[i][j]表示word1的前i个字符和word2的前j个字符组成的两个单词的最长公共子序列的长度。如果当前的两个字符相等,那么dp[i][j] = dp[i-1][j-1] + 1 , 假设[0,i],[0,j]的最后一个字符匹配,则LCS的长度取决于第i-1和j-1个字符;如果不匹配,则需要进行错位比较,也就是说,LCS的长度取决于[i-1]或[j-1](取较长的一个)
Java1:
public class Solution {
public int minDistance(String s1, String s2) {
return s1.length() + s2.length() - 2 * lcs(s1, s2, s1.length(), s2.length());
}
public int lcs(String s1, String s2, int m, int n) {
if (m == 0 || n == 0)
return 0;
if (s1.charAt(m - 1) == s2.charAt(n - 1))
return 1 + lcs(s1, s2, m - 1, n - 1);
else
return Math.max(lcs(s1, s2, m, n - 1), lcs(s1, s2, m - 1, n));
}
}
Java2:
class Solution {
public int minDistance(String word1, String word2) {
int dp[][]=new int[word1.length()+1][word2.length()+1];
for(int i=0;i<word1.length()+1;++i){
for(int j=0;j<word2.length()+1;++j){
if(i==0||j==0)
continue;
if(word1.charAt(i-1)==word2.charAt(j-1))
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);
}
}
return word1.length()+word2.length()-2*dp[word1.length()][word2.length()];
}
}
Java:
class Solution {
public int minDistance(String word1, String word2){
int dp[][]=new int[word1.length()+1][word2.length()+1];
for(int i=0;i<=word1.length();++i){
for(int j=0;j<=word2.length();++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[word1.length()][word2.length()];
}
}
Python1:
class Solution(object):
def minDistance(self, word1, word2):
"""
:type word1: str
:type word2: str
:rtype: int
"""
return len(word1) + len(word2) - 2 * self.lcs(word1, word2) def lcs(self, word1, word2):
len1, len2 = len(word1), len(word2)
dp = [[0] * (len2 + 1) for x in range(len1 + 1)]
for x in range(len1):
for y in range(len2):
dp[x + 1][y + 1] = max(dp[x][y + 1], dp[x + 1][y])
if word1[x] == word2[y]:
dp[x + 1][y + 1] = dp[x][y] + 1
return dp[len1][len2]
Python2:
class Solution(object):
def minDistance(self, word1, word2):
"""
:type word1: str
:type word2: str
:rtype: int
"""
m, n = len(word1), len(word2)
dp = [[0] * (n+1) for _ in xrange(2)]
for i in xrange(m):
for j in xrange(n):
dp[(i+1)%2][j+1] = max(dp[i%2][j+1], \
dp[(i+1)%2][j], \
dp[i%2][j] + (word1[i] == word2[j]))
return m + n - 2*dp[m%2][n]
C++:
class Solution {
public:
int minDistance(string word1, string word2) {
int n1 = word1.size(), n2 = word2.size();
vector<vector<int>> dp(n1 + 1, vector<int>(n2 + 1, 0));
for (int i = 1; i <= n1; ++i) {
for (int j = 1; j <= n2; ++j) {
if (word1[i - 1] == word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return n1 + n2 - 2 * dp[n1][n2];
}
};
C++:
class Solution {
public:
int minDistance(string word1, string word2) {
int n1 = word1.size(), n2 = word2.size();
vector<vector<int>> dp(n1 + 1, vector<int>(n2 + 1, 0));
for (int i = 0; i <= n1; ++i) dp[i][0] = i;
for (int j = 0; j <= n2; ++j) dp[0][j] = j;
for (int i = 1; i <= n1; ++i) {
for (int j = 1; j <= n2; ++j) {
if (word1[i - 1] == word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[n1][n2];
}
};
类似题目:
[LeetCode] 712. Minimum ASCII Delete Sum for Two Strings 两个字符串的最小ASCII删除和
All LeetCode Questions List 题目汇总
[LeetCode] 583. 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 删除两个字符串的不同部分使两个字符串相同,求删除的步数
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...
- [LeetCode] 712. 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. ...
- 【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 ...
- Java实现 LeetCode 583 两个字符串的删除操作(求最长公共子序列问题)
583. 两个字符串的删除操作 给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符. 示例: 输入: " ...
- Leetcode 583.两个字符串的删除操作
两个字符串的删除操作 给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符. 示例 1: 输入: "se ...
- [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. ...
- 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- 利用SQL直接生成模型实体类
在网上找来一个别人写好的,生成实体类的SQL代码 declare @TableName sysname = 'lkxxb' declare @Result varchar(max) = 'public ...
- KVM-virsh常用命令
virsh list #在线VM virsh list --all #所有VM virsh start #开机 virsh shutdown #软关机 virsh destroy #强制关机 virs ...
- 项目Alpha冲刺--10/10
项目Alpha冲刺--10/10 作业要求 这个作业属于哪个课程 软件工程1916-W(福州大学) 这个作业要求在哪里 项目Alpha冲刺 团队名称 基于云的胜利冲锋队 项目名称 云评:高校学生成绩综 ...
- javascript Object and new object() object --构造函数
- 【python】raise_for_status()抛出requests.HTTPError错误
1.首先看下面代码的运行情况 import requests res = requests.get("https://www.csdn.net/eee", headers=head ...
- idea去除mybatis的xml那个恶心的绿色背景
https://my.oschina.net/qiudaozhang/blog/2877536
- 调用图灵API V2 遇到的坑
1:遇到的第一个问题:跨域 解决办法: 第一种:使用query中的jsonp 可惜图灵要求post方法,而jsonp,只能使用get方法,才能跨域,失败 第二种:服务器添加header,可是我怎么去改 ...
- 函数(定义、参数、return、变量、作用域、预解析)
一.函数定义 1.方式一 function 函数名(参数){ 函数体 }——————函数声明的方法 function fn(a){ console.log(a); }: 2.方式二 ...
- 洛谷 P4779 【模板】单源最短路径(标准版) 题解
P4779 [模板]单源最短路径(标准版) 题目背景 2018 年 7 月 19 日,某位同学在 NOI Day 1 T1 归程 一题里非常熟练地使用了一个广为人知的算法求最短路. 然后呢? 100 ...
- 【loj2341】【WC2018】即时战略
题目 交互题: 一开始所有点都是黑的,你需要把所有点变白: explore(u,v)会将u到v路径上的第二个点变白: 一开始只有1号点是白色的,你需要让所有点变白: 对于一条链次数限制\(O(n+lo ...