<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:
   
    //instead check whether s2 is subsquence of s1, but still 47/53, so use map
    bool isSubseq(string &s1,string &s2,int n,int m){
        if(m<0) return 1;
        if(n<0) return 0;
        //Here order of base case matters

        if(s1[n]==s2[m]) return isSubseq(s1,s2,n-1,m-1);
        return isSubseq(s1,s2,n-1,m);
    }
 
    bool isSubseq2(string &s1,string &s2,int n,int m){
        int j=0;
        for(int i=0;i<n && j<m;i++){
            if(s1[i]==s2[j]) j++;
        }
        return j==m;
    }

    int numMatchingSubseq(string s, vector<string>& words) {
        int cnt=0;
        map<string, bool> mp;
        for(auto &x:words){
            if(mp.find(x) != mp.end()) {
				// if that string has value true that means it is subsequence, count++
                if(mp[x] == true) cnt++;
				
                continue; //if not, go for next string
            }
            
            mp[x]=isSubseq(s,x,s.size()-1,x.size()-1);
            // mp[x]=isSubseq2(s,x,s.size(),x.size());
            if(mp[x] == true) cnt+=1;
        }
        
        return cnt;
    }
    
};
43069