<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 {
    pair<int,int> find_lps(string str1)
    {
         int n=str1.length();
        int lps[n];
        lps[0]=0;
        int len=0;
        int maxx=0;
        for(int i=1;i<n;)
        {
            if(str1[i]==str1[len])
            {
                len++;
                lps[i]=len;
                maxx=max(maxx,lps[i]);
                i++;
            }
            else
            {
                if(len>0)
                    len=lps[len-1];
                else
                {
                    lps[i]=0;
                    i++;
                }
            }
        }
        return {lps[n-1],maxx};
    }
public:
    int repeatedStringMatch(string a, string b) 
    {
        string s=b+"#"+a;
        auto [last,maxx]=find_lps(s);
        if(maxx==b.length())return 1;
        
        if(!last)return -1;
        int res=1e9;
        for(int i=-1;i<last;i++)
        {
            int temp=i==-1?0:1;
            int j;
            bool f=0;
            for(j=i+1;;j+=a.length())
            {
                if(j+a.length()>=b.length())break;
                if(b.substr(j,a.length())!=a){f=1;break;}
                temp++;
            }
            if(f)continue;
            if(j==b.length())res=min(res,temp);
            if(b.substr(j)!=a.substr(0,b.length()-j))continue;
            res=min(res,temp+1);
        }  
        return res==1e9?-1:res;
    }
};
106511