<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:
        # subsequecene is ordered weaved list
        # if no sequence return 0
        # can this be imagined as a tree by looking at pairs of characters in the words?
        # starting from a,a you're good and should move forward both characters
        # starting from b,c u have two choices (c,c) and (b, e)
        # i must take the max from my choices
        # how do i know when i'm done? if either pointer points past valid character
        word1_len = len(text1)
        word2_len = len(text2)
        def LCS(l1, l2, memo):
            lett_pair = f'{l1},{l2}'
            if lett_pair in memo:
                return memo[lett_pair]
            if l1 == word1_len or l2 == word2_len:
                return 0
            if text1[l1] == text2[l2]:
                memo[lett_pair] = LCS(l1 + 1, l2 + 1, memo) + 1
            else:
                memo[lett_pair] = max(LCS(l1 + 1, l2, memo), LCS(l1, l2 + 1, memo))
            return memo[lett_pair]
        
        overall_max = 0
        memo = {}
        for i in range(word1_len):
            overall_max = max(overall_max, LCS(i, 0, memo))
        return overall_max
139396