<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:
    bool CheckRepOfA(string a,string b, int ap,int &bp){
        int al=a.length();
        while (ap!=al)
        {
            if(a[ap]!=b[bp]) return false;
            ap++;
            bp++;
        }
        return true;
    }

    int repeatedStringMatch(string a, string b) {
        int al=a.length(), bl=b.length();

        if( al==0 && bl>0) return -1;
        if( bl==0 && al>=0) return 0;

        if(bl<=al){
            if(a.find(b)<al) return 1;
            else if(a.append(a).find(b)<al) return 2;
            else return -1;
        } 

        int bp=0,ap=0,rep=0;

        while (bp!=bl){
        
            if(ap==0 && bp==0 ){
            
                while (ap<al){
                    if(b[bp]==a[ap]){
                        if(CheckRepOfA(a,b,ap,bp))break;
                        bp=0;
                    }
                    ap++;
                }
                if(bp==0) return -1;
            }
            else if((ap>=0 && ap<al) && (bp>=0 && bp<bl) && b[bp]==a[ap]){

                if((bl-bp-1)<al){
                    while (bp!=bl)
                    {
                        if(a[ap]!=b[bp]) return -1;
                        ap++;
                        bp++;
                    }
                }
                else if(!CheckRepOfA(a,b,0,bp) && bp!=bl) return -1;
            }
            else  return -1;
            ap=0;
            rep++;
        }
        
        return rep;
    }
};
225884