<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 count(string s,string t,vector<vector<int>> &dp,int i,int j,vector<vector<int>> &dp1){
        if(i==0 || j==0) return 1;
        if(dp1[i][j]!=-1) return dp1[i][j];
        int a1=0;
        if(s[i-1]==t[j-1]){
            a1=count(s,t,dp,i-1,j-1,dp1);
        }
        if(dp[i][j]==dp[i-1][j] && dp[i][j]==dp[i][j-1]){
            return dp1[i][j]=a1+count(s,t,dp,i-1,j,dp1)+count(s,t,dp,i,j-1,dp1);
        }
        else if(dp[i][j]==dp[i-1][j]){
            return dp1[i][j]=a1+count(s,t,dp,i-1,j,dp1);
        }
        else if(dp[i][j]==dp[i][j-1]){
            return dp1[i][j]=a1+count(s,t,dp,i,j-1,dp1);
        }
      
        return dp1[i][j]=a1;
    }
    
    int numDistinct(string s, string t) {
        int n1=s.length(),n2=t.length();
        vector<vector<int>> dp(n1+1,vector<int>(n2+1,0));
        for(int i=1;i<=n1;i++){
            for(int j=1;j<=n2;j++){
                if(s[i-1]==t[j-1]){
                    dp[i][j]=1+dp[i-1][j-1];
                }
                else{
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        int i1=n1,i2=n2;
        string s1="";
        while(i1>0 && i2>0){
            if(s[i1-1]==t[i2-1]){
                s1+=s[i1-1];
                i1--,i2--;
            }
            else{
                if(dp[i1][i2]==dp[i1-1][i2]){
                    i1--;
                }
                else if(dp[i1][i2]==dp[i1][i2-1]){
                    i2--;
                }
            }
        }
        reverse(s1.begin(),s1.end());
        if(s1!=t) return 0;
        vector<vector<int>> dp1(n1+1,vector<int>(n2+1,-1));
        int c=count(s,t,dp,n1,n2,dp1);
        return c;
    }
};
41253