<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>


from collections import Counter
from functools import lru_cache
class Solution:
    def numDistinct(self, s: str, t: str) -> int:
        #num_sol = [0]

        t = list(t)
        
        validChr = Counter(list(s))
        needChar = Counter(t)
        mapdata = {}
        
        @lru_cache
        def find(i, text):
            print(i, text)
            total_sol = 0
            text = list(text) 
            if text  == t:                
                return 1 
            
            #k = len(text)
            #print(len(text), text)
            next_word = t[len(text)]

            temp = []
            #for i, c in enumerate(s):    
            #print('find', i, len(s), list(range(i, len(s))))             
            for j in range(i, len(s)):
                c = s[j]
                if c == next_word:
                    # do 
                    text.append(c)   

                    needChar[c] -= 1  

                    before = validChr[c]                    
                    #print('remove', c, validChr, text, s[j:], i)
                        
                    
                    validChr[c] -= 1 # Skip next
                    
                    # k = mapdata.get(
                    #     (j+1, tuple(text))
                    #     ,find(j+1, tuple(text))
                    # ) 
                    # mapdata[
                    #     (j+1, tuple(text))
                    # ] = k 
                    #print('find next', j, j+1, s[j+1:])
                    total_sol += find(j+1, tuple(text)) 

                    # Undo
                    text.pop(-1)
                    needChar[c] += 1                     
                    validChr[c] += 1
                    #print('add', c, validChr, text,i)

                    after = validChr[c]
                    assert before == after, ValueError(s, c, text)

                else:
                    pass
                                                    
                assert Counter(list(s[j:])) == validChr, ValueError(
                    c,
                    s[j:],
                    validChr,
                    i,j,
                    text
                )   
                
                if c in validChr:
                    validChr[c] -= 1  
                    #print('remove after', c, validChr, text, s[j+1:])

                    temp.append(c) 
                    if validChr[c] < needChar[c]:   
                        validChr.update(temp) # Rollback   
                        #print('rollback', temp)                                   
                        return total_sol  

            validChr.update(temp) # Rollback 
            #print('rollback', temp)
            return total_sol
                             
        total = find(0, tuple([]))        
        return total
        


        
27118