Complete-Preparation

🎉 One-stop destination for all your technical interview Preparation 🎉

View the Project on GitHub

Deletions and insertions require to convert string A to B 🌟🌟

Solution

Code

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;
}