Leetcode 72: Edit Distance
Given two strings str1 and str2, your task is to determine the minimum number of operations required to transform str1 into str2. The allowed operations are: Insert a character, Delete a character, and Replace a character.
Problem
Approach
Steps
Complexity
Input: You are given two strings str1 and str2.
Example: str1 = 'kitten', str2 = 'sitting'
Constraints:
• 0 <= str1.length, str2.length <= 500
• str1 and str2 consist of lowercase English letters.
Output: Return the minimum number of operations needed to transform str1 into str2.
Example: 3
Constraints:
• The output should be a single integer.
Goal: Calculate the minimum number of operations needed.
Steps:
• Use dynamic programming to store the results of subproblems.
• For each pair of indices (i, j), consider the operations: insert, delete, or replace.
• Build the solution iteratively using the recurrence relation.
Goal: Constraints on the problem.
Steps:
• 0 <= str1.length, str2.length <= 500
• The strings consist of lowercase English letters.
Assumptions:
• The input strings are non-empty.
• Both strings contain only lowercase alphabets.
• Input: str1 = 'kitten', str2 = 'sitting'
• Explanation: We need 3 operations to convert 'kitten' into 'sitting': 1) Replace 'k' with 's', 2) Replace 'e' with 'i', 3) Insert 'g'.
• Input: str1 = 'flaw', str2 = 'lawn'
• Explanation: We need 2 operations to convert 'flaw' into 'lawn': 1) Remove 'f', 2) Insert 'n'.
Approach: The problem can be solved using dynamic programming, where we calculate the minimum number of operations to transform substrings of str1 into substrings of str2.
Observations:
• The problem can be broken down into smaller subproblems.
• We can use dynamic programming to solve this efficiently.
• Use a 2D array to store intermediate results.
Steps:
• Define a 2D dp array where dp[i][j] stores the minimum number of operations to convert the first i characters of str1 to the first j characters of str2.
• Initialize the dp array for base cases where one of the strings is empty.
• Fill the dp array using the recurrence relation for each pair of indices (i, j).
Empty Inputs:
• If either str1 or str2 is empty, the minimum operations are the length of the other string.
Large Inputs:
• Handle the case when both strings are of maximum length (500).
Special Values:
• Consider cases where str1 and str2 are already equal, in which case the answer is 0.
Constraints:
• Ensure the solution is optimized for the problem's constraints.
string a, b;
vector<vector<int>> memo;
int dp(int i, int j) {
if(i == a.size() && j == b.size()) return 0;
if(i == a.size()) return b.size() - j;
if(j == b.size()) return a.size() - i;
if(memo[i][j] != -1) return memo[i][j];
int ans = 0;
if(a[i] != b[j]) {
ans = min({dp(i+1,j), dp(i,j+1), dp(i+1,j+1)}) + 1;
} else {
ans = dp(i + 1, j+ 1);
}
return memo[i][j] = ans;
}
int minDistance(string word1, string word2) {
this->a = word1;
this->b = word2;
memo.resize(a.size(), vector<int>(b.size(), -1));
return dp(0, 0);
}
1 : Variable Declaration
string a, b;
Declares two string variables `a` and `b` to store the input words.
2 : Array Initialization
vector<vector<int>> memo;
Declares a 2D vector `memo` to store memoized results for the `dp` function.
3 : Function Declaration
int dp(int i, int j) {
Declares a recursive function `dp` to calculate the minimum edit distance between two substrings starting at indices `i` and `j`.
4 : Base Case
if(i == a.size() && j == b.size()) return 0;
Base case: If both strings are exhausted, the edit distance is 0.
5 : Base Case
if(i == a.size()) return b.size() - j;
Base case: If `a` is exhausted, the remaining edit distance is the length of `b`.
6 : Base Case
if(j == b.size()) return a.size() - i;
Base case: If `b` is exhausted, the remaining edit distance is the length of `a`.
7 : Memoization
if(memo[i][j] != -1) return memo[i][j];
Checks if the result for the current `i` and `j` is already memoized.
8 : Variable Initialization
int ans = 0;
Initializes `ans` to store the minimum edit distance.
9 : Conditional
if(a[i] != b[j]) {
Checks if the current characters at indices `i` and `j` are different.
10 : Min Calculation
ans = min({dp(i+1,j), dp(i,j+1), dp(i+1,j+1)}) + 1;
If the characters are different, calculates the minimum edit distance by considering insertion, deletion, and substitution operations.
11 : Conditional
} else {
If the characters are the same.
12 : Recursive Call
ans = dp(i + 1, j+ 1);
If the characters are the same, move to the next characters in both strings.
13 : Memoization
return memo[i][j] = ans;
Stores the calculated minimum edit distance in the memoization table and returns it.
14 : Function Declaration
int minDistance(string word1, string word2) {
Declares the main function `minDistance` to calculate the minimum edit distance between two words.
15 : Variable Assignment
this->a = word1;
Assigns the input word1 to the class member variable `a`.
16 : Variable Assignment
this->b = word2;
Assigns the input word2 to the class member variable `b`.
17 : Array Initialization
memo.resize(a.size(), vector<int>(b.size(), -1));
Initializes the `memo` array with -1 to indicate uncalculated values.
18 : Function Call
return dp(0, 0);
Calls the `dp` function to calculate the minimum edit distance starting from the beginning of both words.
Best Case: O(m * n)
Average Case: O(m * n)
Worst Case: O(m * n)
Description: Where m and n are the lengths of str1 and str2, respectively.
Best Case: O(m * n)
Worst Case: O(m * n)
Description: We need to store the dp array of size m * n.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus