Given two strings str1 and str2, find the minimum number of operations required to convert str1 into str2. The allowed operations are:
- Insert a character
- Remove a character
- Replace a character
The problem is to calculate the edit distance between the two strings.
Input/Output Examples
plaintext
Example 1:
Input: str1 = "kitten", str2 = "sitting"
Output: 3
Explanation: The minimum operations required are:
Replace k with s
Replace e with i
Insert g at the end
Example 2:
Input: str1 = "sunday", str2 = "saturday"
Output: 3
Explanation: The minimum operations required are:
Insert a after s
Insert t after a
Replace n with r
Approach to find the Edit Distance between two strings
- Dynamic Programming Approach:
- Use a 2D DP table dp[i][j]whereiis the index instr1andjis the index instr2.dp[i][j]represents the minimum edit distance between the firsticharacters ofstr1and the firstjcharacters ofstr2.
- If str1[i-1] == str2[j-1], no operation is required, sodp[i][j] = dp[i-1][j-1].
- If str1[i-1] != str2[j-1], the three possible operations (insert, delete, replace) are considered, and the minimum operation cost is taken:- Insert: dp[i][j-1] + 1
- Delete: dp[i-1][j] + 1
- Replace: dp[i-1][j-1] + 1
 
- Insert: 
 
- Use a 2D DP table 
- Algorithm:
- Initialize a DP table of size (len(str1)+1) x (len(str2)+1)where the first row and the first column represent converting one string to an empty string.
- Traverse through the strings, and for each character pair, update the DP table based on whether the characters match or an operation (insert, delete, replace) is required.
- The final answer will be stored in dp[len(str1)][len(str2)].
 
- Initialize a DP table of size 
- Edge Cases:
- If either string is empty, the edit distance is the length of the other string (since all characters need to be inserted or deleted).
- If both strings are equal, the edit distance is 0.
 
C++ Program to find the Edit Distance between two strings
cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Function to find the minimum edit distance between two strings
int editDistance(string str1, string str2) {
    int m = str1.length();
    int n = str2.length();
    // Create a DP table with size (m+1) x (n+1)
    vector<vector<int>> dp(m + 1, vector<int>(n + 1));
    // Initialize the base cases
    for (int i = 0; i <= m; i++) {
        for (int j = 0; j <= n; j++) {
            if (i == 0) {
                dp[i][j] = j; // If str1 is empty, insert all characters of str2
            } else if (j == 0) {
                dp[i][j] = i; // If str2 is empty, remove all characters of str1
            } else if (str1[i - 1] == str2[j - 1]) {
                dp[i][j] = dp[i - 1][j - 1]; // Characters match, no operation needed
            } else {
                // Consider insert, delete, and replace operations
                dp[i][j] = 1 + min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]});
            }
        }
    }
    // Return the edit distance between the two strings
    return dp[m][n];
}
int main() {
    string str1 = "kitten";
    string str2 = "sitting";
    int result = editDistance(str1, str2);
    cout << "Edit Distance: " << result << endl;
    return 0;
}
Java Program to find the Edit Distance between two strings
cpp
public class EditDistance {
    // Function to find the minimum edit distance between two strings
    public static int editDistance(String str1, String str2) {
        int m = str1.length();
        int n = str2.length();
        // Create a DP table with size (m+1) x (n+1)
        int[][] dp = new int[m + 1][n + 1];
        // Initialize the base cases
        for (int i = 0; i <= m; i++) {
            for (int j = 0; j <= n; j++) {
                if (i == 0) {
                    dp[i][j] = j; // If str1 is empty, insert all characters of str2
                } else if (j == 0) {
                    dp[i][j] = i; // If str2 is empty, remove all characters of str1
                } else if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1]; // Characters match, no operation needed
                } else {
                    // Consider insert, delete, and replace operations
                    dp[i][j] = 1 + Math.min(dp[i - 1][j - 1], 
                                            Math.min(dp[i - 1][j], dp[i][j - 1]));
                }
            }
        }
        // Return the edit distance between the two strings
        return dp[m][n];
    }
    public static void main(String[] args) {
        String str1 = "kitten";
        String str2 = "sitting";
        int result = editDistance(str1, str2);
        System.out.println("Edit Distance: " + result);
    }
}
Python Program to find the Edit Distance between two strings
python
# Function to find the minimum edit distance between two strings
def edit_distance(str1, str2):
    m, n = len(str1), len(str2)
    # Create a DP table with size (m+1) x (n+1)
    dp = [[0] * (n + 1) for _ in range(m + 1)]
    # Initialize the base cases
    for i in range(m + 1):
        for j in range(n + 1):
            if i == 0:
                dp[i][j] = j  # If str1 is empty, insert all characters of str2
            elif j == 0:
                dp[i][j] = i  # If str2 is empty, remove all characters of str1
            elif str1[i - 1] == str2[j - 1]:
                dp[i][j] = dp[i - 1][j - 1]  # Characters match, no operation needed
            else:
                # Consider insert, delete, and replace operations
                dp[i][j] = 1 + min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1])
    # Return the edit distance between the two strings
    return dp[m][n]
# Example usage
if __name__ == "__main__":
    str1 = "kitten"
    str2 = "sitting"
    result = edit_distance(str1, str2)
    print("Edit Distance:", result)
Time Complexity:
- O(m * n): We compute the values for all entries in the DP table, where mis the length ofstr1andnis the length ofstr2.
Space Complexity:
- O(m * n): We use a 2D DP table of size (m + 1) x (n + 1).