Complete-Preparation

πŸŽ‰ One-stop destination for all your technical interview Preparation πŸŽ‰

View the Project on GitHub

Longest common subsequence 🌟🌟

Recursive Approach

Code

int f(string& a, string& b, int i, int j)
{
    if (i < 0 || j < 0) return 0;

    if (a[i] == b[j]) return 1 + f(a, b, i - 1, j - 1);
    return dp[i][j] = max(f(a, b, i, j - 1), f(a, b, i - 1, j));
}

int getLengthOfLCS(string& str1, string& str2)
{
    int n = str1.size(), m = str2.size();
    return f(str1, str2, n - 1, m - 1);
}

Memoization

Code

int f(string& a, string& b, int i, int j, vector<vector<int>>& dp)
{
    if (i < 0 || j < 0)
        return 0;
    if (dp[i][j] != -1)
        return dp[i][j];
    if (a[i] == b[j])
        return 1 + f(a, b, i - 1, j - 1, dp);
    return dp[i][j] = max(f(a, b, i, j - 1, dp), f(a, b, i - 1, j, dp));
}

int getLengthOfLCS(string& str1, string& str2)
{
    int n = str1.size(), m = str2.size();
    vector<vector<int>> dp(n, vector<int>(m, -1));
    return f(str1, str2, n - 1, m - 1, dp);
}

Tabulation

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));
    // 0'th row and 0'th column are already 0, we can skip them
    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];
}

Space Optimized Tabulation

Code

int getLengthOfLCS(string& a, string& b)
{
    int n = a.size(), m = b.size();
    vector<int> prev(m + 1, 0), curr(m + 1, 0);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (a[i - 1] == b[j - 1])
                curr[j] = 1 + prev[j - 1];
            else
                curr[j] = max(curr[j - 1], prev[j]);
        }
        prev = curr;
    }

    return prev[m];
}