<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:
        dp = {} # (text1_start, text2_start) to max_options

        def _imp(text1_start: int, text2_start: int) -> int:
            """5,3
            dp: (4,0): 1  (3,0): 1  (4,2): 1  (3,2): 1  (2,0): 2  (1,0): 2

            t1_s 0
            t2_s 0
            letter a
            sub_len - max(2, )
            pos = 0

            t1_s 1
            t2_s 0
            letter b
            sub_len - max(2, --)
            return 2

            t1_s 2
            t2_s 0
            letter c
            sub_len - max(1, 1+1)
            pos = 1
            return 2

            t1_s 3
            t2_s 2
            letter d
            sub_len - max(1, --)
            pos = 1
            return 1

            t1_s 4
            t2_s 2
            letter e
            sub_len - max(0, 0+1)
            pos = 2
            return 1

            t1_s 5
            t2_s 3
            return 0

            t1_s 5
            t2_s 2
            return 0 

            t1_s 3
            t2_s 0
            letter d
            sub_len - max(1, --)
            return 1

            t1_s 4
            t2_s 0
            letter e
            sub_len - max(0, 0+1)
            return 1

            t1_s 5
            t2_s 0
            return 0

            t1_s 5
            t2_s 3
            return 0
            """
            if text1_start >= len(text1) or text2_start >= len(text2):
                return 0

            if (text1_start, text2_start) in dp:
                return dp[(text1_start, text2_start)]
            
            letter = text1[text1_start]
            sub_len = _imp(text1_start + 1, text2_start)
            if letter in text2[text2_start:]:
                text2_pos = text2.index(letter, text2_start)
                sub_len = max(sub_len, _imp(text1_start + 1, text2_pos + 1) + 1)

            dp[(text1_start, text2_start)] = sub_len
            return sub_len

        return _imp(0, 0)

        
242366