<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:
        self.text1 = text1
        self.text2 = text2
        self.memo = {}

        # self.count = 200
        result = self.iterate(0, 0)
        # print(self.memo)
        return result

    # def iterate(self, text1: str, text2: str) -> int:
    def iterate(self, idx1: int, idx2: int) -> int:
        # self.count -= 1
        # if self.count < 0:
        #     return 0
        # if len(text1) == 0 or len(text2) == 0:
        #     return 0
        if idx1 >= len(self.text1) or idx2 >= len(self.text2):
            return 0
        
        key = f"{idx1}-{idx2}"
        # key = f"{self.text1[idx1:]}-{self.text2[idx2:]}"
        # print(key)
        if key in self.memo:
            return self.memo[key]
        
        remove_1 = self.iterate(idx1+1, idx2)

        _1_in_2 = self.text2.find(self.text1[idx1], idx2)
        # print(f"{_1_in_2} = ({self.text2[idx2:]}).find({self.text1[idx1]})")
        if _1_in_2 >= 0:
            # print(f"found {key} = {idx1}|{_1_in_2} => {self.text1[idx1:idx1+10]} {self.text2[_1_in_2:_1_in_2+10]}")
            keep_1 = 1 + self.iterate(idx1+1, _1_in_2+1)
        else:
            keep_1 = 0
        result = max(remove_1, keep_1)

        # print(f"{key}: max({remove_1}, {keep_1})")

        self.memo[key] = result
        return result
98943