Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/583.html
583. Delete Operation for Two Strings (Medium)
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.
Solution 1. Longest Common Subsequence
A variation of LCS problem. The result should be M + N - 2 * len(LCS)
.
// OJ: https://leetcode.com/problems/delete-operation-for-two-strings
// Time: O(MN)
// Space: O(MN)
class Solution {
public:
int minDistance(string word1, string word2) {
int M = word1.size(), N = word2.size();
vector<vector<int>> dp(M + 1, vector<int>(N + 1, 0));
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (word1[i] == word2[j]) dp[i + 1][j + 1] = dp[i][j] + 1;
else dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
}
}
return M + N - 2 * dp[M][N];
}
};
Solution 2. DP
Let dp[i][j]
be the minimum number of steps required for A[0..(i-1)]
and B[0..(j-1)]
.
dp[i][j] = dp[i - 1][j - 1] if A[i - 1] == B[j - 1]
= 1 + min(dp[i - 1][j], dp[i][j - 1]) if A[i - 1] != B[j - 1]
dp[i][0] = i
dp[0][j] = j
// OJ: https://leetcode.com/problems/delete-operation-for-two-strings/
// Time: O(MN)
// Space: O(MN)
class Solution {
public:
int minDistance(string A, string B) {
int M = A.size(), N = B.size();
vector<vector<int>> dp(M + 1, vector<int>(N + 1, INT_MAX));
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 (A[i - 1] == B[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[M][N];
}
};
Solution 3. DP with Space Optimization
Since dp[i][j]
is only dependent on dp[i-1][j-1]
, dp[i-1][j]
and dp[i][j-1]
, we can reduce the size of the dp
array from M * N
to N
, with an additional variable saving dp[i-1][j-1]
.
// OJ: https://leetcode.com/problems/delete-operation-for-two-strings/
// Time: O(MN)
// Space: O(min(M,N))
class Solution {
public:
int minDistance(string A, string B) {
int M = A.size(), N = B.size();
if (M < N) swap(M, N), swap(A, B);
vector<int> dp(N + 1, INT_MAX);
for (int i = 0; i <= M; ++i) {
int prev;
for (int j = 0; j <= N; ++j) {
int cur = dp[j];
if (i == 0 || j == 0) dp[j] = i + j;
else if (A[i - 1] == B[j - 1]) dp[j] = prev;
else dp[j] = 1 + min(dp[j], dp[j - 1]);
prev = cur;
}
}
return dp[N];
}
};
-
class Solution { public int minDistance(String word1, String word2) { int length1 = word1.length(), length2 = word2.length(); int longestCommonSubsequenceLength = longestCommonSubsequence(word1, word2); int delete1 = length1 - longestCommonSubsequenceLength; int delete2 = length2 - longestCommonSubsequenceLength; return delete1 + delete2; } public int longestCommonSubsequence(String text1, String text2) { int length1 = text1.length(), length2 = text2.length(); int[][] dp = new int[length1 + 1][length2 + 1]; for (int i = 1; i <= length1; i++) { char c1 = text1.charAt(i - 1); for (int j = 1; j <= length2; j++) { char c2 = text2.charAt(j - 1); if (c1 == c2) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); } } int commonLength = dp[length1][length2]; return commonLength; } } ############ class Solution { public int minDistance(String word1, String word2) { int m = word1.length(), n = word2.length(); int[][] dp = new int[m + 1][n + 1]; for (int i = 1; i <= m; ++i) { dp[i][0] = i; } for (int j = 1; j <= n; ++j) { dp[0][j] = j; } for (int i = 1; i <= m; ++i) { for (int j = 1; j <= n; ++j) { if (word1.charAt(i - 1) == word2.charAt(j - 1)) { dp[i][j] = dp[i - 1][j - 1]; } else { dp[i][j] = 1 + Math.min(dp[i - 1][j], dp[i][j - 1]); } } } return dp[m][n]; } }
-
// OJ: https://leetcode.com/problems/delete-operation-for-two-strings // Time: O(MN) // Space: O(MN) class Solution { public: int minDistance(string s, string t) { int M = s.size(), N = t.size(); vector<vector<int>> dp(M + 1, vector<int>(N + 1)); for (int i = 0; i < M; ++i) { for (int j = 0; j < N; ++j) { if (s[i] == t[j]) dp[i + 1][j + 1] = 1 + dp[i][j]; else dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]); } } return M + N - 2 * dp[M][N]; } };
-
class Solution: def minDistance(self, word1: str, word2: str) -> int: m, n = len(word1), len(word2) dp = [[0] * (n + 1) for _ in range(m + 1)] for i in range(1, m + 1): dp[i][0] = i for j in range(1, n + 1): dp[0][j] = j for i in range(1, m + 1): for j in range(1, n + 1): 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[-1][-1] ############ class Solution(object): def minDistance(self, word1, word2): """ :type word1: str :type word2: str :rtype: int """ dp = [[0] * (len(word2) + 1) for _ in range(len(word1) + 1)] for i in range(1, len(word2) + 1): dp[0][i] = i for i in range(1, len(word1) + 1): dp[i][0] = i for i in range(len(word1)): for j in range(len(word2)): if word1[i] == word2[j]: dp[i + 1][j + 1] = dp[i][j] else: dp[i + 1][j + 1] = min(dp[i][j + 1] + 1, dp[i + 1][j] + 1) return dp[-1][-1]
-
func minDistance(word1 string, word2 string) int { m, n := len(word1), len(word2) dp := make([][]int, m+1) for i := range dp { dp[i] = make([]int, n+1) dp[i][0] = i } for j := range dp[0] { dp[0][j] = j } for i := 1; i <= m; i++ { for j := 1; j <= n; 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[m][n] } func min(a, b int) int { if a < b { return a } return b }
-
function minDistance(word1: string, word2: string): number { const m = word1.length; const n = word2.length; const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); for (let i = 1; i <= m; i++) { for (let j = 1; j <= n; j++) { if (word1[i - 1] === word2[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]); } } } const max = dp[m][n]; return m - max + n - max; }
-
impl Solution { pub fn min_distance(word1: String, word2: String) -> i32 { let (m, n) = (word1.len(), word2.len()); let (word1, word2) = (word1.as_bytes(), word2.as_bytes()); let mut dp = vec![vec![0; n + 1]; m + 1]; for i in 1..=m { for j in 1..=n { dp[i][j] = if word1[i - 1] == word2[j - 1] { dp[i - 1][j - 1] + 1 } else { dp[i - 1][j].max(dp[i][j - 1]) } } } let max = dp[m][n]; ((m - max) + (n - max)) as i32 } }