<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 {
    // long long table[1002][1002];
    /*int calc(string &s, string &t, int i, int j){
        if(j==t.length()){
            return 1;
        }
        if( i>=s.length()  ){
            return 0;
        }
        if(table[i][j]!=-1){
            return table[i][j];
        }
        int op1=0,op2=0;

        op1 = calc(s,t,i+1,j);
        if(s[i]==t[j]){
            op2 = calc(s,t,i+1,j+1);
        }
        return table[i][j] = op1+op2;
    }*/
    // dp[i][j] = (if eq) (dp[i+1][j+1]) + dp[i+1][j];
public:
    int numDistinct(string s, string t) {

        // for(int i=0;i<1002;i++){
        //     for(int j=0;j<1002;j++){
        //         table[i][j]=0;
        //     }
        // }
        vector<vector<unsigned int>> table(1002,vector<unsigned int>(1002,0));
        for(int i=0;i<=s.length();i++){
            table[i][t.length()]=1;
        }


        // for(int i=0;i<t.length();i++){
        //     if( s[s.length()-1] == t[i])
        //     {
        //         cout<<"a";
        //         table[s.length()-1][i] = 1;
        //     }
        //     else
        //         table[s.length()-1][i] = 0;
        // }
        for(int i=s.length()-1;i>=0;i--){
            for(int j=t.length()-1;j>=0;j--){
                
                table[i][j] = table[i+1][j];
                if(s[i] == t[j]){
                    table[i][j] += table[i+1][j+1];
                }
            }
        }
        
        // for(int i=0;i<s.length()+1;i++){
        //     for(int j=0;j<t.length()+1;j++){
        //         cout<<" " <<table[i][j];
        //     }
        //     cout<<endl;
        // }

        return table[0][0];
        
    }
};
196578