<p>Given two strings <code>text1</code> and <code>text2</code>, return <em>the length of their longest <strong>common subsequence</strong>. </em>If there is no <strong>common subsequence</strong>, return <code>0</code>.</p>

<p>A <strong>subsequence</strong> of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.</p>

<ul>
	<li>For example, <code>&quot;ace&quot;</code> is a subsequence of <code>&quot;abcde&quot;</code>.</li>
</ul>

<p>A <strong>common subsequence</strong> of two strings is a subsequence that is common to both strings.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> text1 = &quot;abcde&quot;, text2 = &quot;ace&quot; 
<strong>Output:</strong> 3  
<strong>Explanation:</strong> The longest common subsequence is &quot;ace&quot; and its length is 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> text1 = &quot;abc&quot;, text2 = &quot;abc&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> The longest common subsequence is &quot;abc&quot; and its length is 3.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> text1 = &quot;abc&quot;, text2 = &quot;def&quot;
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no such common subsequence, so the result is 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= text1.length, text2.length &lt;= 1000</code></li>
	<li><code>text1</code> and <code>text2</code> consist of only lowercase English characters.</li>
</ul>

class Solution {
public:
    
    int solveUsingRecursion(string a, string b, int i, int j){
        //base case
        if(i==a.length()) return 0;
        if(j==b.length()) return 0;

        int ans = 0;
        if(a[i]==b[j]) return 1 + solveUsingRecursion(a, b, i+1, j+1);
        else{
            return 0 + max(solveUsingRecursion(a, b, i, j+1), solveUsingRecursion(a, b, i+1, j));
        }
        return ans;
    }

    int solveUsingMem(string& a, string& b, int i, int j, vector<vector<int>>& dp){
        //base case
        if(i==a.length()) return 0;
        if(j==b.length()) return 0;

        if(dp[i][j] != -1) return dp[i][j];
        
        int ans = 0;
        if(a[i]==b[j]){
            ans = 1 + solveUsingMem(a, b, i+1, j+1, dp);
        } 
        else{
            ans = 0 + max(solveUsingMem(a, b, i, j+1, dp), solveUsingMem(a, b, i+1, j, dp));
        }
        dp[i][j] = ans;
        return dp[i][j];
    }

    int solveUsingTab(string a, string b){
        vector<vector<int>> dp(a.length()+1, vector<int>(b.length()+1, 0));

        for(int i= a.length()-1; i>=0; i--){
            for(int j=b.length()-1; j>=0; j--){
                int ans = 0;
                if(a[i] == b[j])
                    ans = 1 + dp[i+1][j+1];
                else{
                    ans = 0 + max(dp[i][j+1], dp[i+1][j]);
                }
                dp[i][j] = ans;
            }
        }
        return dp[0][0];
    }
    
    int longestCommonSubsequence(string text1, string text2) {
        // int i=0;
        // int j=0;
        // vector<vector<int>> dp(text1.length(), vector<int>(text2.length(), -1));
        // return solveUsingMem(text1, text2, i, j, dp); 

        return solveUsingTab(text1, text2);
        
    }
};
23278