<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 lcs(string a,string b,int m, int n){
       int mat[m+1][n+1];
       for(int i=0;i<=m;i++){
        for(int j=0;j<=n;j++){
            if(i==0 || j==0){
                mat[i][j]=0;
            }
            else if(a[i-1]==b[j-1]){
                mat[i][j]=1+mat[i-1][j-1];
            }
            else{
                mat[i][j]=max(mat[i][j-1],mat[i-1][j]);
            }
        }
       }
    //    return mat[m][n];
       string ans="";
       while(m>0 && n>0){
        if(a[m-1]==b[n-1]){
            ans=ans+a[m-1];
            m--;
            n--;
        }
        else {
        if(mat[m][n-1]>mat[m-1][n]){
            n--;
        }
        else{
            m--;
        }
        }
       }
       reverse(ans.begin(),ans.end());
       cout<<ans;
       return ans.length();




    //       int mat[m+1][n+1];
    //     //   for(int i=0;i<n;i++){
    //     //     mat[0][i]=0;
    //     //   }
    //     //   for(int j=0;j<m;j++){
    //     //     mat[j][0]=0;
    //     //   }
    //       for(int i=0;i<=m;i++){
    //         for(int j=0;j<=n;j++){
    //             if(i==0 || j==0){
    //                 mat[i][j]=0;
    //             }
    //             else if(a[i-1]==b[j-1]){
    //                 mat[i][j]=1+mat[i-1][j-1];
    //             }
    //             else{
    //                 mat[i][j]=max(mat[i-1][j],mat[i][j-1]);
    //             }
    //         }
    //       }
    //     return mat[m][n];

    //     // if(m==0 || n==0){
    //     //     return 0;
    //     // }
    //     // if(a[m-1]==b[n-1]){
    //     //     return 1+lcs(a,b,m-1,n-1);
    //     // }
    //     // else{
    //     //     return max(lcs(a,b,m-1,n),lcs(a,b,m,n-1));
    //     // }
    // }
    }

    int longestCommonSubsequence(string text1, string text2) {
        return lcs(text1,text2,text1.length(),text2.length());
    }
    
};
13226