<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 f(int ind1, int ind2, string &s, string &t, map<pair<int,int>,int> &dp){
        
    //     if(ind2==0) return 1;
    //     if(ind1==0) return 0;
    //     if(dp.find({ind1,ind2})!=dp.end()) return dp[{ind1,ind2}];
    //     int take=0;
    //     int nottake=0;
    //     if(s[ind1-1]==t[ind2-1]){
    //         take=f(ind1-1,ind2-1,s,t,dp);
    //         nottake=f(ind1-1,ind2,s,t,dp);
    //     }
    //     else{
    //         nottake=f(ind1-1,ind2,s,t,dp);
    //     }
    //     return dp[{ind1,ind2}]=take+nottake;
    // }


    // int numDistinct(string s, string t) {
    //     map<pair<int,int>,int> dp;
    //     int ans=f(s.size(),t.size(),s,t,dp);
    //     return ans;
    // }

    int numDistinct(string s, string t) {
        int n=s.size();
        int m=t.size();
        //vector<vector<int>> dp(n+1,vector<int>(m+1));
        map<long long int,map<long long int,long long int>> dp;
        for(int i=0;i<m;i++) dp[0][i]=0;
        for(int i=0;i<n;i++) dp[i][0]=1;
        for(int ind1=1;ind1<=n;ind1++){
            for(int ind2=1;ind2<=m;ind2++){
                int take=0;
                int nottake=0;
                if(s[ind1-1]==t[ind2-1])
                take=dp[ind1-1][ind2-1];
                nottake=dp[ind1-1][ind2];
                dp[ind1][ind2]=(long long int)take+nottake;
            }
        }        
    return dp[n][m];
    }
};
155158