🎉 One-stop destination for all your technical interview Preparation 🎉
size of string A - length of LCS of A and B.
size of string B - length of LCS of A and B.
insertion + deletion - lcs(A,B) = size(A) + size(B) - 2*lcs(A,B)
int getLengthOfLCS(string& a, string& b)
{
int n = a.size(), m = b.size();
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i - 1] == b[j - 1])
dp[i][j] = 1 + dp[i - 1][j - 1];
else
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
}
}
return dp[n][m];
}
int canYouMake(string &str, string &ptr)
{
int lenLCS = getLengthOfLCS(str,ptr);
int deletions = str.size() - lenLCS;
int insertions = ptr.size() - lenLCS;
return deletions + insertions;
}