<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>

class Solution:
    def numDistinct(self, s: str, t: str) -> int:
        # Edge case
        if len(s) < len(t):
            return 0
        
        # Constructing DP
        dp = [[0] * (len(t) + 1) for _ in range(len(s) + 1)]
        prev = [0] * (len(t) + 1)
        prev[0] = 1

        # Base cases
        for row in range(len(s) + 1):
            dp[row][0] = 1

        # Recursive case, filling the dp
        for i in range(1, len(s) + 1):
            curr = [0] * (len(t) + 1)
            curr[0] = prev[0]
            for j in range(1, len(t) + 1):
                # Carry forward the previous value (not matching current s[i-1] with t[j-1])
                curr[j] = prev[j]
                # If characters match, add the number of subsequences found by matching the previous characters
                if s[i - 1] == t[j - 1]:
                    curr[j] += prev[j - 1]
            prev = curr

        # Returning correct value from dp
        return prev[-1]
25169