<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 {
public:
    int dp[1002][1002];
    //i => s and j => t , only changing parameters are i and j so size of dp 2d array is max i * max j (or max_i + 1 * max_j + 1 due to index shifting)
    int f(int i, int j, string& s, string& t)
    {
        if(j < 0) return 1;
        if(i < 0) return 0;
        if(dp[i][j] != -1) return dp[i][j];
        int take = 0, nottake = 0;
        if(s[i] == t[j]) take = f(i - 1, j - 1, s, t);
        nottake = f(i - 1, j, s, t);
        return dp[i][j] = take + nottake;
    }

    //Top Down => Recursion ka Ulta :)
    int tab(string s, string t) {
        int m = s.size(), n = t.size();
        vector<vector<int>>dpTab(m + 1, vector<int>(n + 1));

        //Base Case
        //j = 0 (j < 0 ka index shifting) => return 1
        for(int i = 0; i <= m; i++) dpTab[i][0] = 1;
        // i = 0 => return 0;
        for(int j = 1; j <= n; j++) dpTab[0][j] = 0; //iski jarurat nahi thi as pehle se 0 se initialized hai => But agar aise bhi kiya toh chalega lekin dont start by j = 0 as it overwrites the value of dp[0][0] from 1 to 0 => 1 hi hona chahiye as dp[0][0] indicates ki both string are empty "" "" toh no. of distinct subsequences of s which equals t => 1

        //Recurrence shuru hua tha m - 1 , n - 1 se so tabulation starts from 0/1 as ulta of recursion
        for(int i = 1; i <= m; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                int take = 0, nottake = 0;
                if(s[i - 1] == t[j - 1]) take = dpTab[i - 1][j - 1];
                nottake = dpTab[i - 1][j];

                dpTab[i][j] = long(take) + long(nottake); 
            }
        }

        return dpTab[m][n]; //m - 1 , n - 1 hona chahiye for full string size of both lekin index shifting :)

    }
    int numDistinct(string s, string t) {
        // memset(dp, -1, sizeof(dp));
        // int m = s.size(), n = t.size();
        //return f(m - 1, n - 1, s, t);

        return tab(s, t);
    }
};
25721