<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:
        # intuition: come up with all subsequences for each string
        # then keep track of which ones are common
        # likely will be lots of repeated work
        #
        # to find all subsequences:
        # for each character, choose to delete or not delete

        # memoization
        cache: Dict[Tuple[int,int]] = {}
        def solve(p1: int, p2: int) -> int:
            if (p1, p2) in cache:
                return cache[(p1, p2)]

            # base case: one or more substrings are empty
            if p1 == len(text1) or p2 == len(text2):
                cache[(p1, p2)] = 0
                return cache[(p1, p2)]

            # case 1: don't include p1
            case1 = solve(p1 + 1, p2)

            # case 2: include p1, make sure it exists in p2
            index = text2.find(text1[p1], p2)
            case2 = 0
            if index != -1:
                case2 = 1 + solve(p1 + 1, index + 1)

            cache[(p1, p2)] = max(case1, case2)
            return cache[(p1, p2)]

        return solve(0, 0)

        # result: int = 0
        # text1_subseqs: Set[str] = set()
        # text2_subseqs: Set[str] = set()

        # def find_subseqs(stored_set: Set[str], text: str, prefix: str):
        #     if len(prefix) > 0:
        #         stored_set.add(prefix)
        #     if len(text) == 0:
        #         return
        #     elif len(text) > 1:
        #         find_subseqs(stored_set, text[1:], prefix)
        #         find_subseqs(stored_set, text[1:], prefix + text[0])
        #     else:
        #         find_subseqs(stored_set, '', prefix)
        #         find_subseqs(stored_set, '', prefix + text[0])

        # find_subseqs(text1_subseqs, text1, '')
        # find_subseqs(text2_subseqs, text2, '')
        # print(text1_subseqs)
        # print(text2_subseqs)

        # common = list(text1_subseqs & text2_subseqs)
        # for sub in common:
        #     print(sub)
        #     if len(sub) > result:
        #         result = len(sub)
        # return result
249721