<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 Solution:
    def numMatchingSubseq(self, s: str, words: List[str]) -> int:
        s = self.cache_s(s)  # {char: [positions]}
        print(s)

        def is_sub(word):
            idx = -1
            for ch in word:  # O(50)

                positions = s[ch]
                idx = self.find(positions, idx + 1)  # O(log N)
                if idx == -1:
                    return False
            return True

        cnt = 0
        for word in words:  # O(M)
            if is_sub(word):
                print(word)
                cnt += 1
        return cnt

    def cache_s(self, s):
        res = defaultdict(list)
        for idx, char in enumerate(s):
            res[char].append(idx)
        return res

    def find(self, nums, start):
        # positions are sorted
        # need to find start or or return next num in nums after start.
        # if not such num return -1
        l = 0
        r = len(nums)
        # nums = [0,2,5,7, 12]
        # [3] start = 5

        while l < r:
            m = (l + r) // 2  # 0
            if nums[m] == start:
                return nums[m]
            elif nums[m] < start:
                l = m + 1  # l = 1
            else:
                r = m
        if r == len(nums):
            return - 1
        return nums[r]
21100