<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:
    def longestCommonSubsequence(self, text1: str, text2: str) -> int:
        
        # A1: DP Top-Down (Recustion) (Memoization)

        memo = {ii : {jj : (None if jj < len(text2) and ii < len(text1) else 0) for jj in range(len(text2) + 1)} for ii in range(len(text1) + 1)}
        memo[len(text1)][len(text2)] = 0

        # print(memo)

        counter1 = 0
        counter2 = 0

        def rec(p1, p2):

            nonlocal counter1
            nonlocal counter2

            # print(p1, p2, memo[p1][p2])

            counter1 += 1

            if memo[p1][p2] is not None:
                return memo[p1][p2]

            counter2 += 1

            # case 1 (not a part of optimal solution)
            case_1 = rec(p1 + 1, p2)

            # case 2
            first_occurence_in_text_2 = text2.find(text1[p1], p2)
            if first_occurence_in_text_2 != -1:
                case_2 = 1 + rec(p1 + 1, first_occurence_in_text_2 + 1)
            else:
                case_2 = 0

            memo[p1][p2] = max(case_1, case_2)

            return memo[p1][p2]

        rr =  rec(0, 0)

        print(counter1, counter2)

        for ii in range(len(text1)):
            for jj in range(len(text1)):
                x= ii * jj

        return rr
205591