ronith
11/3/2018 - 6:32 AM

Edit Distance

Given two strings str1 and str2 and below operations that can performed on str1. Find minimum number of edits (operations) required to convert ‘str1’ into ‘str2’.

Insert Remove Replace All of the above operations are of equal cost.

Examples:

Input: str1 = "geek", str2 = "gesek" Output: 1 We can convert str1 into str2 by inserting a 's'.

Input: str1 = "cat", str2 = "cut" Output: 1 We can convert str1 into str2 by replacing 'a' with 'u'.

Input: str1 = "sunday", str2 = "saturday" Output: 3 Last three and first characters are same. We basically need to convert "un" to "atur". This can be done using below three operations. Replace 'n' with 'r', insert t, insert a

Algo: The idea is process all characters one by one staring from either from left or right sides of both strings. Let us traverse from right corner, there are two possibilities for every pair of character being traversed. m: Length of str1 (first string) n: Length of str2 (second string) If last characters of two strings are same, nothing much to do. Ignore last characters and get count for remaining strings. So we recur for lengths m-1 and n-1. Else (If last characters are not same), we consider all operations on ‘str1’, consider all three operations on last character of first string, recursively compute minimum cost for all three operations and take minimum of three values. Insert: Recur for m and n-1 Remove: Recur for m-1 and n Replace: Recur for m-1 and n-1

#include<iostream>
using namespace std;
int main()
 {
    int t;
    cin>>t;
    while (t-->0) {
        int n,m;
        cin>>n>>m;
        string s,t;
        cin>>s>>t;
        
        int dp[n+1][m+1];
        
        for (int i=0;i<=n;i++) {
            for (int j=0;j<=m;j++) {
                if (i==0)
                    dp[i][j]= j;
                else if (j==0)
                    dp[i][j]= i;
                else if (s[i-1]==t[j-1])
                    dp[i][j]= dp[i-1][j-1];
                else
                    dp[i][j]= 1+min(dp[i][j-1],//for inserting
                    min(dp[i-1][j],//remove
                    dp[i-1][j-1]));//replace
            }
        }
        cout<< dp[n][m]<<endl;
    }
	return 0;
}