#include<bits/stdc++.h>
using namespace std;
string str1, str2;
int minimum(int a, int b, int c)
{
return min(a, min(b, c));
}
int med(int n, int m)
{
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(str1[i-1] == str2[j-1])
dp[i][j] = dp[i-1][j-1];
else
dp[i][j] = 1 + minimum(dp[i][j-1], dp[i-1][j], dp[i-1][j-1]);
}
}
return dp[n][m];
}
int main()
{
cout << "Enter First String : ";
cin >> str1;
cout << "Enter Second String : ";
cin >> str2;
int n = str1.size();
int m = str2.size();
cout << "Minimum Edit Distance : " << med(n, m) << "\n";
return 0;
}
Comments
Post a Comment