<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 solve(int ind1, int ind2, string text1, string text2,
              vector<vector<int>>& dp) {
        if (ind1 == 0 || ind2 == 0)
            return 0;
        if (dp[ind1][ind2] != -1)
            return dp[ind1][ind2];
        if (text1[ind1 - 1] == text2[ind2 - 1])
            return dp[ind1][ind2] =
                       1 + solve(ind1 - 1, ind2 - 1, text1, text2, dp);
        else
            return dp[ind1][ind2] =
                       max(solve(ind1 - 1, ind2, text1, text2, dp),
                           solve(ind1, ind2 - 1, text1, text2, dp));
    }
    int longestCommonSubsequence(string text1, string text2) {
        if(text1 == text2) return text1.length();
           int n1 = text1.length(), n2 = text2.length();
           /* vector<vector<int>>dp(n1+1, vector<int>(n2+1,-1));
           return solve(n1, n2, text1, text2, dp); */


       /* vector<vector<int>>dp(n1 + 1, vector<int>(n2 + 1, 0));
        for (int i = 0; i <= n1; i++)
            dp[i][0] = 0;
        for (int i = 0; i <= n2; i++)
            dp[0][i] = 0;
        for (int i = 1; i <= n1; i++) {
            for (int j = 1; j <= n2; j++) {
                if (text1[i - 1] == text2[j - 1])
                    dp[i][j] = 1 + dp[i - 1][j - 1]; 
                else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
        return dp[n1][n2]; */

        vector<int>dp(n2 + 1, 0);
        for (int i = 0; i <= n1; i++)
            dp[0] = 0;
        for (int i = 1; i <= n1; i++) {
            vector<int>temp(n2 + 1, 0);
            for (int j = 1; j <= n2; j++) {
                if (text1[i - 1] == text2[j - 1])
                    temp[j] = 1 + dp[j - 1]; 
                else temp[j] = max(dp[j], temp[j - 1]);
            }
            dp = temp;
        }
        return dp[n2];

    }
};
19070