🎉 One-stop destination for all your technical interview Preparation 🎉
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
int lcs(int x, int y, string s1, string s2)
{
int dp[x + 1][y + 1];
for (int i = 0; i < x + 1; i++)
{
for (int j = 0; j < y + 1; j++)
{
if (i == 0 || j == 0)
dp[i][j] = 0;
else if (s1[i - 1] == s2[j - 1])
dp[i][j] = 1 + dp[i - 1][j - 1];
else
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
return dp[x][y];
}
int minOperations(string str1, string str2)
{
int m = str1.size();
int n = str2.size();
int len = lcs(m, n, str1, str2);
return ((m - len) + (n - len));
}