<p>Given a string <code>s</code> and an array of strings <code>words</code>, return <em>the number of</em> <code>words[i]</code> <em>that is a subsequence of</em> <code>s</code>.</p>

<p>A <strong>subsequence</strong> of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.</p>

<ul>
	<li>For example, <code>&quot;ace&quot;</code> is a subsequence of <code>&quot;abcde&quot;</code>.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;abcde&quot;, words = [&quot;a&quot;,&quot;bb&quot;,&quot;acd&quot;,&quot;ace&quot;]
<strong>Output:</strong> 3
<strong>Explanation:</strong> There are three strings in words that are a subsequence of s: &quot;a&quot;, &quot;acd&quot;, &quot;ace&quot;.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;dsahjpjauf&quot;, words = [&quot;ahjpjau&quot;,&quot;ja&quot;,&quot;ahbwzgqnuk&quot;,&quot;tnmlanowax&quot;]
<strong>Output:</strong> 2
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= words.length &lt;= 5000</code></li>
	<li><code>1 &lt;= words[i].length &lt;= 50</code></li>
	<li><code>s</code> and <code>words[i]</code> consist of only lowercase English letters.</li>
</ul>

class TrieNode:
    def __init__(self):
        self.children = defaultdict(TrieNode)
        self.is_end = 0


class Trie:
    def __init__(self):
        self.root = TrieNode()
        self.memo = defaultdict(set)

    def insert(self, word):
        root = self.root
        for c in word:
            if root == self.root:
                self.memo[c].add(root.children[c])
            root = root.children[c]
        root.is_end += 1
        return


class Solution:
    def numMatchingSubseq(self, s: str, words: List[str]) -> int:
        n = len(words)
        trie = Trie()
        for i in range(n):
            trie.insert(words[i])
        memo = trie.memo
        res = 0
        # seen = set()
        for c in s:
            if c in memo:
                old = memo[c]
                memo[c] = set()
                for node in old:
                    if node.is_end > 0:
                        res += node.is_end
                    for nxt in node.children:
                        memo[nxt].add(node.children[nxt])
        return res


# class Solution(object):
#     def numMatchingSubseq(self, S, words):
#         ans = 0
#         heads = [[] for _ in range(26)]
#         for word in words:
#             it = iter(word)
#             heads[ord(next(it)) - ord('a')].append(it)

#         # print(heads)
#         for letter in S:
#             letter_index = ord(letter) - ord('a')
#             old_bucket = heads[letter_index]
#             heads[letter_index] = []

#             while old_bucket:
#                 it = old_bucket.pop()
#                 nxt = next(it, None)
#                 if nxt:
#                     heads[ord(nxt) - ord('a')].append(it)
#                 else:
#                     ans += 1

#         return ans
27900