<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 {
public:
    int numMatchingSubseq(string s, vector<string>& words) {
        int count=0;
        unordered_map<char, vector<int>> mpp;
        unordered_map<string, int> mpp2;
        for(int i=0; i<s.length(); i++){
            if(mpp.find(s[i])==mpp.end()){
                vector<int> tmp;
                tmp.push_back(i);
                mpp[s[i]]=tmp;
            }else{
                mpp[s[i]].push_back(i);
            }
        }
        for(int i=0; i<words.size(); i++){
            if(mpp2.find(words[i])!=mpp2.end()) count+=mpp2[words[i]];
            else{
                mpp2[words[i]]=isSub(mpp,s,words[i]);
                count+= mpp2[words[i]];
            }
        }
        return count;
    }
    int isSub(unordered_map<char, vector<int>>& mpp, string s1, string s2){
        int len1=s1.length(), len2=s2.length(), currPtr=-1, found=0;
        char tmp;
        for(int i=0; i<len2; i++){
            tmp=s2[i];
            if(mpp.find(tmp)==mpp.end())    return 0;
            found=0;
            for(int j=0; j<mpp[tmp].size(); j++){
                if(mpp[tmp][j]>currPtr){
                    currPtr=mpp[tmp][j];
                    found=1;
                    break;
                }
            }
            if(found!=1)    return 0;
        }
        return 1;
    }
};
345075