<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:
    def repeatedStringMatch(self, a: str, b: str) -> int:
        return self.KMP(a, b)
        # return self.brutal(a, b)

    def KMP(self, a: str, b: str) -> int:
        def buildLongestSuffixPrefix(pattern: str):
            lsp = [0] * len(pattern)
            preLsp = 0
            i = 1
            while i < len(pattern):
                if pattern[i] == pattern[preLsp]:
                    lsp[i] = preLsp + 1 # 
                    preLsp += 1 # Need to be acc.
                    i += 1
                elif preLsp == 0:
                    lsp[i] = 0
                    i += 1
                else:
                    preLsp = lsp[preLsp-1]
            return lsp

        def doKmp(strings, pattern, lsp):
            i, j = 0, 0
            res = []
            while i < len(strings):
                if strings[i] == pattern[j]:
                    i += 1
                    j += 1
                elif j == 0:
                    i += 1
                else:
                    j = lsp[j-1]
                if j == len(pattern):
                    return i - len(pattern)
            return -1

        times, left = divmod(len(b), len(a))
        if left > 0:
            times += 1
        lsp = buildLongestSuffixPrefix(b)
        for i in range(2):
            repeated_a = a * (times + i)
            index = doKmp(repeated_a, b, lsp)
            if index >= 0:
                return times + i
        return -1

    def brutal(self, a: str, b: str) -> int:
        A, B = len(a), len(b)
        if a == b:
            return 1
        times, left = divmod(B, A)
        if left > 0:
            times += 1
        for i in range(2):
            if b in a * (times + i): # times + i, the max of i is `1` which mean double
                return times + i
        return -1
        
17100