leetcode 583. 两个字符串的删除操作
题目链接:两个字符串的删除操作
两个字符串可以相互删除
版本一:
- 确定dp数组及下标的含义
dp[i][j]
:以i-1为结尾的字符串word1,和以j-1为结尾的字符串word2,想要达到相等,所需要删除元素的最少次数 - 确定递推公式
(1)当word1[i - 1] 与 word2[j - 1]相同:
dp[i][j] = dp[i - 1][j - 1]
(2)当word1[i - 1] 与 word2[j - 1]不相同:
情况一:删word1[i - 1],最少操作次数为dp[i - 1][j] + 1
情况二:删word2[j - 1],最少操作次数为dp[i][j - 1] + 1
情况三:同时删word1[i - 1]和word2[j - 1],操作的最少次数为dp[i - 1][j - 1] + 2
则dp[i][j] = min({dp[i - 1][j - 1] + 2, dp[i - 1][j] + 1, dp[i][j - 1] + 1})
由于同时删word1[i - 1]和word2[j - 1],相当于dp[i][j-1] 基础上不考虑word2[j - 1],删除word1[i - 1],则
dp[i][j - 1] + 1 = dp[i - 1][j - 1] + 2。所以递推公式简化为:
dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1)
- dp数组初始化
vector<vector<int>> dp(word1.size() + 1, vector<int>(word2.size() + 1));
for (int i = 0; i <= word1.size(); i++) dp[i][0] = i;
for (int j = 0; j <= word2.size(); j++) dp[0][j] = j;
- 确定遍历顺序
从左到右,从上到下
class Solution {
public:
int minDistance(string word1, string word2) {
vector<vector<int>> dp(word1.size() + 1, vector<int>(word2.size() + 1));
for (int i = 0; i <= word1.size(); i++) dp[i][0] = i;
for (int j = 0; j <= word2.size(); j++) dp[0][j] = j;
for (int i = 1; i <= word1.size(); i++) {
for (int j = 1; j <= word2.size(); j++) {
if (word1[i - 1] == word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1);
}
}
}
return dp[word1.size()][word2.size()];
}
};
版本二:
求出两个字符串的最长公共子序列长度,除了最长公共子序列之外的字符都是必须删除的,用两个字符串的总长度减去两个最长公共子序列的长度就是删除的最少步数。
class Solution {
public:
int minDistance(string word1, string word2) {
vector<vector<int>> dp(word1.size() + 1, vector<int>(word2.size() + 1, 0));
for (int i = 1; i <= word1.size(); i++) {
for (int j = 1; j <= word2.size(); 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 word1.size() + word2.size() - 2*dp[word1.size()][word2.size()];
}
};
leetcode 72. 编辑距离
题目链接:编辑距离
- 确定dp数组及下标的含义
dp[i][j]
:以i-1为结尾的字符串word1,和以j-1为结尾的字符串word2,最近的编辑距离 - 确定递推函数
(1)如果word1[i - 1] == word2[j - 1]:不用编辑,dp[i][j] = dp[i - 1][j - 1]
(2)如果word1[i - 1] != word2[j - 1]:(一个字符串添加一个元素,相当于另一个字符串删除一个元素,所以删除和添加可以转换)三种情况取最小值
操作一:word1删除一个元素,dp[i][j] = dp[i - 1][j] + 1
操作二:word2删除一个元素,dp[i][j] = dp[i ][j -1] + 1
操作三:替换元素,word1替换word1[i - 1],使其与word2[j - 1]相同,此时不用增删元素,dp[i][j] = dp[i - 1][j - 1] + 1
所以递推公式:
if (word1[i - 1] == word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
}
else {
dp[i][j] = min({dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]}) + 1;
}
- dp数组初始化
for (int i = 0; i <= word1.size(); i++) dp[i][0] = i;
for (int j = 0; j <= word2.size(); j++) dp[0][j] = j;
- 确定遍历顺序
从前到后,从上到下
class Solution {
public:
int minDistance(string word1, string word2) {
vector<vector<int>> dp(word1.size() + 1, vector<int>(word2.size() + 1, 0));
for (int i = 0; i <= word1.size(); i++) dp[i][0] = i;
for (int j = 0; j <= word2.size(); j++) dp[0][j] = j;
for (int i = 1; i <= word1.size(); i++) {
for (int j = 1; j <= word2.size(); j++) {
if (word1[i - 1] == word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
}
else {
dp[i][j] = min({dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]}) + 1;
}
}
}
return dp[word1.size()][word2.size()];
}
};
时间复杂度: O(n * m)
空间复杂度: O(n * m)