<p>Given two strings <code>a</code> and <code>b</code>, return <em>the minimum number of times you should repeat string </em><code>a</code><em> so that string</em> <code>b</code> <em>is a substring of it</em>. If it is impossible for <code>b</code>​​​​​​ to be a substring of <code>a</code> after repeating it, return <code>-1</code>.</p>

<p><strong>Notice:</strong> string <code>&quot;abc&quot;</code> repeated 0 times is <code>&quot;&quot;</code>, repeated 1 time is <code>&quot;abc&quot;</code> and repeated 2 times is <code>&quot;abcabc&quot;</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> a = &quot;abcd&quot;, b = &quot;cdabcdab&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> We return 3 because by repeating a three times &quot;ab<strong>cdabcdab</strong>cd&quot;, b is a substring of it.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> a = &quot;a&quot;, b = &quot;aa&quot;
<strong>Output:</strong> 2
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= a.length, b.length &lt;= 10<sup>4</sup></code></li>
	<li><code>a</code> and <code>b</code> consist of lowercase English letters.</li>
</ul>

class Solution {
public:
   int strStr(string haystack, string needle) {
        vector<int>a(needle.size(),0);
        int req=0;
        for(int i=1;i<needle.size();i++){
            if(needle[i]==needle[req]){
                req++;
                a[i]=req;
            }
            else {
                while(req>0&&needle[i]!=needle[req]){
                    req=a[req-1];
                }
                if(needle[i]==needle[req]){
                    req++;
                    a[i]=req;
                }
            }
            // cout<<a[i]<<" ";
        }
        // cout<<endl;
        int j=-1;
        int i;
        for( i=0;i<haystack.size();i++){
            if(j==needle.size()-1) break;
            if(haystack[i]==needle[j+1]){
                j++;
                // cout<<j<<" "<<i<<" "<<haystack[i]<<" "<<needle[j]<<endl;
            }
            else{
                // cout<<i<<" not satisfied "<<endl;
                while(j!=-1&&haystack[i]!=needle[j+1]){
                     j=a[j]-1;
                    //  cout<<j<<" changed"<<endl;
                }
                if(haystack[i]==needle[j+1]){
                    j++;
                }
            }
        }
        if(j!=needle.size()-1) return -1;
        else return i-j-1;
    }
    int repeatedStringMatch(string a, string b) {
        int n=a.length();
        // int m=b.length();
        string req=a;
        int ans=1;
        //fidn whether there are lemts are not
        map<char,int>m;
        for(int i=0;i<a.length();i++){
            m[a[i]]++;
        }
        for(int j=0;j<b.length();j++){
            if(m.find(b[j])!=m.end()){}
            else{return -1;}
        }
        while(req.length()<b.length()){
             req=req+a;
             ans++;
            //  cout<<req<<endl;
        }
        int k=0;
        while(strStr(req,b)==-1){
                req+=a;
                ans++;
                k++;
                // cout<<"inside this"<<req<<endl;
                if(k>3) return -1;
        }
        return ans;

    }
};
74373