class Solution {
    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;
            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;
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter