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

# This is a recursive approach to this solution. For each word, in the worst case, the recursive function may traverse all characters of s, which gives a complexity of O(N) for one word. Therefore, for W words, the total time complexity becomes O(W * N).

        # def is_subsequence(word, s):
        #     if not word: return True
        #     if not s: return False
        #     if word[0] == s[0]:
        #         return is_subsequence(word[1:], s[1:])
        #     else:
        #         return is_subsequence(word, s[1:])
        
        # count = 0
        # for w in words:
        #     if is_subsequence(w, s):
        #         count += 1
        
        # return count

        total = 0
        hashmap = {}

        # Count the occurrences of each word in the words list
        for word in words:
            if word not in hashmap:
                hashmap[word] = 1
            else:
                hashmap[word] += 1

        # Check each unique word in stringmap
        for word, count in hashmap.items():
            i, j = 0, 0  # Initialize pointers for 's' and 'word'
            while i < len(s) and j < len(word):
                if s[i] == word[j]:
                    j += 1
                i += 1

            # If the entire word was found as a subsequence
            if j == len(word):
                total += count

        return total

# Total Time Complexity: O(W + W' * n)

# W is the total number of words in the words list.
# W' is the number of unique words in the words list.
# n is the length of the string s.


18500