<p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>

<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;rabbbit&quot;, t = &quot;rabbit&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate &quot;rabbit&quot; from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;babgbag&quot;, t = &quot;bag&quot;
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate &quot;bag&quot; from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length, t.length &lt;= 1000</code></li>
	<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>

import numpy as np
class Solution:
    def numDistinct(self, s: str, t: str) -> int:
        n1 = len(s)
        n2 = len(t)
        if n1==n2:
            return 1 if s==t else 0
        if len(s) < len(t):
            return 0
        # a(i, j) = the nbr of solutions where s[i:] contains t[j:] and 0 s[i] exactly matches t[j]
        # b(i, j) = the nbr of solutions where s[i:] contains t[j:] , 0<=i<n1
        # the boarderline conditions: 
        # b(i, n2-1) = how many times s[i:] contains  element t[n2-1]
        # b(n1-1, j) = how many times s[n1-1] contains t[j:]
        # we want to know b(0, 0) = a(0, 0) + a(1, 0) + ... + a(n1-1, 0)
        # b(i, j) = a(i, j) + a(i + 1, j) + ... + a(n1-1, j)
        # a(i, j) = b(i+1, j+1) if s[i] = t[j]
        # so b(i, j) = b(i+1, j+1)*I(s[i]=t[j]) + b(i+2, j+1)*I(s[i+1]=t[j]) + ... b(n1-1, j+1) 
        # and in particular, if 
        # try this initiate a numpy array(n1*n2) dim such that digit_check[i,j] = 1 if s[i] == t[j] else 0
        
        b = np.zeros((n1, n2), dtype = np.int64)
        digit_check = np.zeros((n1, n2), dtype = np.int64)
        for i in range(n1):
            for j in range(n2):
                if s[i] == t[j]:
                    digit_check[i,j] =  1
        # border conditions
        b[-1, 0:n2-1] = 0
        b[-1, -1] = 1 if digit_check[-1,-1] == 1 else 0
        for i in range(n1-2, -1, -1):
            b[i, n2-1] = b[i+1, n2-1] + 1 if digit_check[i, n2-1] == 1 else b[i+1, n2-1]
        # forward steps
        for j in range(n2-2, -1, -1): 
            # t[j:] has length n2-j. s[i:] has length n1-i. n1-i>= n2-j equals i<=n1-n2+j
            for i in range(n1-n2+j, -1, -1): 
                b[i, j]= (b[i+1:n1, j+1] * digit_check[i:n1-1, j]).sum()
                # b[i, j] = sum([b[k+1, j+1] if s[k] == t[j] else 0 for k in range(i, n1-1)])
        # print(b)
        return b[0,0]

        
    

42708