<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>



import sys
class Solution:

    # recursion
    def _solveLCSHelper(self,text1,text2,i,j):
        if i==len(text1) or j==len(text2):
            return 0

        if text1[i]==text2[j]:
            return 1+self._solveLCSHelper(text1,text2,i+1,j+1)
        else:
            ans1=self._solveLCSHelper(text1,text2,i+1,j)
            ans2=self._solveLCSHelper(text1,text2,i,j+1)
            return max(ans1,ans2)


    # top down
    def _solveLCSHelperdp(self,text1,text2,i,j,dp):
        if i==len(text1) or j==len(text2):
            dp[i][j]=0
            return dp[i][j]
        if dp[i][j]!=-sys.maxsize:
            return dp[i][j]

        if text1[i]==text2[j]:
            dp[i][j]=1+self._solveLCSHelperdp(text1,text2,i+1,j+1,dp)
            return dp[i][j]
        else:
            ans1=self._solveLCSHelperdp(text1,text2,i+1,j,dp)
            ans2=self._solveLCSHelperdp(text1,text2,i,j+1,dp)
            dp[i][j]=max(ans1,ans2)
            return dp[i][j] 
    

    # bottom up
    def _solveLCSHelperdpBottomUp(self,text1,text2):
        dp=[[-sys.maxsize for j in range(len(text2)+1)] for i in range(len(text1)+1)]

        for i in range(len(text1)+1):
            dp[i][-1]=0
        for j in range(len(text2)+1):
            dp[-1][j]=0

        for i in range(len(text1)-1,-1,-1):
            for j in range(len(text2)-1,-1,-1):
                if text1[i]==text2[j]:
                    dp[i][j]=1+dp[i+1][j+1]
                else:
                    dp[i][j]=max(dp[i+1][j],dp[i][j+1])
        return dp[0][0]

        return dp[0][0]
    def longestCommonSubsequence(self, text1: str, text2: str) -> int:
        # return self._solveLCSHelper(text1,text2,0,0)


        # dp=[[-sys.maxsize for j in range(len(text2)+1)] for i in range(len(text1)+1)]
        # return self._solveLCSHelperdp(text1,text2,0,0,dp)
        return self._solveLCSHelperdpBottomUp(text1,text2)
        
69523