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

import bisect
class Solution:
    def numMatchingSubseq(self, s: str, words: List[str]) -> int:
        positions = {}
        count = 0
        for i in range(len(s)):
            if s[i] in positions.keys():
                positions[s[i]].append(i)
            else:
                positions[s[i]] = [i]

        for j in range(len(words)):
            curr_word = words[j]
            #last_position = -1
            last_position = 0
            is_subsequence = True
            for k in range(len(curr_word)):
                curr_letter = curr_word[k]
                if curr_letter in positions.keys():
                    found = False
                    # for l in positions[curr_letter]:
                    #     if last_position < l:
                    #         last_position = l 
                    #         found = True
                    #         break
                    # if found == False:
                    #     # this word is not a subsequence
                    #     is_subsequence = False
                    #     break
                    l = bisect_left(positions[curr_letter], last_position)
                    if l >= len(positions[curr_letter]):
                        is_subsequence = False
                        break
                    last_position = positions[curr_letter][l] + 1
                else:
                    is_subsequence = False
            if is_subsequence == True:
                count += 1
        return count
        # def isSubseq(word):
        #     curr = 0
        #     for symbol in word:
        #         ind = bisect_left(places[symbol], curr)
        #         if ind >= len(places[symbol]):
        #             return False
        #         curr = places[symbol][ind] + 1
            
        #     return True
        
        # places = defaultdict(list)
        # for i, symbol in enumerate(S):
        #     places[symbol].append(i)
        
        # return sum(isSubseq(word) for word in words)


                    
20600