<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 findmatch(string a , string b , int i){
        while(i<a.size()){
            if(a[i]==b[0])
            break;
            else 
            i++;
        }
        if(i==a.size())
        return -1;
        return i;
    }
    int repeatedStringMatch(string a, string b) {
        if(b.size()==0||a==b)
        return 1;
        int i=0; int j=0; int cnt=0; int cng=0;
        int ele; ele=findmatch(a,b,0); j=ele;
        if(ele==-1)
        return -1;
        while(i<b.size()){
            if(a[j]==b[i]){
                if(j==0)
                cnt++;
                j=(j+1)%a.size();
                i++;
            }
            else{   
            cng++;
            cnt=0;        
            ele=findmatch(a,b,ele+1);
            if(ele==-1)
            return -1;
            j=ele;
            i=0;
            }
        }
        if(a.size()<b.size()&&a[0]==b[0]&&cng==0)
        return cnt;
        else if(a.size()<b.size()&&a[0]!=b[0]||(a.size()<b.size()&&a[0]==b[0]&&cng!=0))
        return cnt+1;
        else if(a.size()>=b.size()&&a.find(b)!=string::npos)
        return 1;
        else 
        return 2;

    }
};
221293