<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:
    
    # The most brute-force approach would be to compare every subsequence
    # in `text1` with each subsequence of `text2`. 
    #
    # Recursively, we would iterate over the indices of `text1` and `text2`.
    # If the characters at the indices matched, we would increment both
    # indices. If they did not, we branch, and check if we can find a match
    # by incrementing one, the other, or both. 
    #
    # This gives us our recurrence relation. 
    #
    # Our bottom-up solution would just have `0` as the base cases. 
    # As we build our solution, we will add to the cache. 
    def longestCommonSubsequence(self, text1: str, text2: str) -> int:
        
        # Iterative, bottom-up DP solution. 
        
        # Note that we will need an extra row and column to accomodate the
        # base cases. 
        """
        m = len(text1)
        n = len(text2)

        dp = defaultdict(int)
        for i in range(1, m + 1):
            for j in range(1, n + 1):
                if text1[i - 1] == text2[j - 1]:
                    dp[(i, j)] = 1 + dp[(i - 1, j - 1)]
                else:
                    dp[(i, j)] = max(dp[(i - 1, j - 1)],
                                     dp[(i    , j - 1)],
                                     dp[(i - 1, j    )])
        
        return dp[(m, n)]
        """

        # Recursive, top-down DP solution.
        m = len(text1)
        n = len(text2)

        dp = {}
        def rec(i, j):
            if i == -1 or j == -1:
                return 0
            if (i, j) in dp:
                 return dp[(i, j)]
            if text1[i] != text2[j]:
                dp[(i, j)] = max(rec(i - 1, j - 1),
                                 rec(i    , j - 1),
                                 rec(i - 1, j    ))
            else:
                dp[(i, j)] = 1 + rec(i - 1, j - 1)
            return dp[(i, j)]
        
        return rec(m - 1, n - 1)
290173