1143. Longest Common Subsequence

PHOTO EMBED

Sun Apr 23 2023 07:04:14 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

class Solution {
public:
    int mxlcs(string &t1, string &t2, int i, int j,  vector<vector<int>> & dp)
    {
        if(i==t1.size()||j==t2.size()) return 0;
        if(dp[i][j]!=-1) return dp[i][j];
        int p=0;
        if(t1[i]==t2[j]) 
        {
            p = 1+mxlcs(t1, t2, i+1, j+1, dp);
        }
        else p = max(mxlcs(t1, t2, i+1, j, dp), mxlcs(t1, t2, i, j+1, dp));
        return dp[i][j]=p;
    }
    
    int longestCommonSubsequence(string text1, string text2) {
        vector<vector<int>> dp(text1.size(), vector<int>(text2.size(), -1));
        int ans=mxlcs(text1, text2, 0, 0, dp);
        return ans;
    }
};
content_copyCOPY

https://leetcode.com/problems/longest-common-subsequence/