<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:
        # we can ideally use DP in 2d space
        # - DP[i, j] = LCS that starts ar text1[i] and text2[j]. then:
        # if the two chars match:
        # - DP[i, j] = 1 + DP[i+1, j+1]
        # else:
        # - DP[i, j] = max(DP[i+1, j], DP[i, j+1])
        # note that for DP[i, j], we don't require i and j to match, just that it's the LCS from then on.
        # we also look either one to the right, or down, or both. so we can start populating init conditions
        # along the bottom row, and the right column.

        dp = {}
        n, m = len(text1), len(text2)

        # init
        if text1[n-1] == text2[m-1]:
            dp[(n-1, m-1)] = 1
        else:
            dp[(n-1, m-1)] = 0 

        # populate from bottom row to top row, right to left
        for i in list(range(n))[::-1]: # row
            for j in list(range(m))[::-1]: # col
                if text1[i] == text2[j]:
                    if i < n-1 and j < m-1:
                        # we should also take the max here, assuming we do not actually want to use both chars
                        dp[(i, j)] = max(1 + dp[(i+1, j+1)], dp[(i+1, j)], dp[(i, j+1)])
                    elif i < n-1: # j is already at the end / right
                        dp[(i, j)] = max(1, dp[(i+1, j)]) # need the max, we can either choose the chars, or not!
                    elif j < m-1: # we are in bottom row, must look right
                        dp[(i, j)] = max(1, dp[(i, j+1)])
                else:
                    if i < n-1 and j < m-1:
                        dp[(i, j)] = max(dp[(i+1, j)], dp[(i, j+1)], dp[(i+1, j+1)]) # need to add third choice!
                    elif i < n-1: # j is already at the end / right
                        dp[(i, j)] = dp[(i+1, j)]
                    elif j < m-1: # we are in bottom row, must look right
                        dp[(i, j)] = dp[(i, j+1)]
        
        return dp[(0, 0)]




        
198236