<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 findSubstring(string s, string p) {
        // cout << s << "  " << p << endl;
        int i, j, k, m, n;
        n = s.length();
        m = p.length();
        int a[m], b[m + 1];
        a[0] = 0;
        i = 0;
        j = 1;
        while (j < m) {
            if (p[i] == p[j]) {
                a[j] = i + 1;
                i = i + 1;
                j = j + 1;
            } else {
                if (i == 0) {
                    a[j] = 0;
                    j = j + 1;
                } else {
                    i = a[i - 1];
                }
            }
        }
        // for (i = 0; i < m; i++) {
        //     cout << a[i] << "  ";
        // }
        // cout << endl;
        b[0] = 0;
        for (i = 0; i < m; i++) {
            b[i + 1] = a[i];
        }
        p = "a" + p;
        j = 0;
        i = 0;
        while (i < n) {
            //cout << i << " " << j << endl;
            if (s[i] == p[j + 1]) {
                i++;
                j++;
            } else {
                if (j == 0) {
                    i++;
                    continue;
                } else {
                    j = b[j];
                }
            }
            if (j == m) {
                return i - m;
            }
        }
        return -1;
    }
    int repeatedStringMatch(string a, string b) {
        int i, j, k, u, v, n, m;
        string k1;
        m = a.length();
        n = b.length();
        if (m >= n) {
            a = a + a;
            j = findSubstring(a, b);
            if (j != -1) {
                if (j + n > m) {
                    return 2;
                } else {
                    return 1;
                }
            }
        } else {
            u = ceil((float)n / (float)m) * 2;
            k1 = a;
            for (i = 2; i <= u; i++) {
                a = a + k1;
            }
            j = findSubstring(a, b);
            if (j != -1) {
                k = m - j;
                n = n - k;
                return ceil((float)n / (float)m) + 1;
            }
        }
        return -1;
    }
};
308526