<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 {
private:
    int count(vector<vector<char>> &symbols, string &s, string &t, int i, int j, vector<vector<int>> &memo) {
        if(j == 0) {
            return 1;
        }
        if(i == 0) {
            return 0;
        }
        if(memo[i][j] != -1) {
            return memo[i][j];
        }

        int result = count(symbols, s, t, i - 1, j, memo);

        if(symbols[i][j] == 'D' && s[i-1] == t[j-1]) {
            result += count(symbols, s, t, i - 1, j - 1, memo);
        }

        memo[i][j] = result;  // Memoize the result
        return result;
    }

public:
    int numDistinct(string s, string t) {
        int i, j;

        vector<vector<int>> LCS(s.length()+1, vector<int> (t.length()+1));
        vector<vector<char>> symbols(s.length()+1, vector<char> (t.length()+1, '&'));

        //  Generating LCS and symbols that has the [][] that came from 1+LCS
        for(i=0; i<=t.length(); i++) {
            LCS[0][i] = 0;
        }
        for(j=0; j<=s.length(); j++) {
            LCS[j][0] = 0;
        }
        for(i=1; i<=s.length(); i++) {
            for(j=1; j<=t.length(); j++) {
                if(s[i-1] == t[j-1]) {
                    LCS[i][j] = 1+LCS[i-1][j-1];
                    symbols[i][j] = 'D';
                }
                else {
                    if(LCS[i-1][j] >= LCS[i][j-1]) {
                        LCS[i][j] = LCS[i-1][j];
                    }
                    else {
                        LCS[i][j] = LCS[i][j-1];
                    }
                }
            }
        }

        vector<vector<int>> memo(s.length() + 1, vector<int>(t.length() + 1, -1));
        return count(symbols, s, t, s.length(), t.length(), memo);
    }
};
36076