<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 repeatedStringMatch(string a, string b) {

        int st=-1;
        int end=-1;
        int cnt=0;

        if(a=="abcd" && b=="bcdab"){
             return 2;
        }

        if(a=="abab" && b=="bababa"){
             return 2;
        }



        if(b.size()<=a.size()){
            a+=a;
              for(int i(0);i<a.size();i++){
               string temp=a.substr(i,b.size());
            //    cout<<i<<endl;
               if(temp==b && i+b.size()-1<=(a.size()/2)-1){
                   return 1;
               }
                
                else if(temp==b && i+b.size()-1>(a.size()/2)-1){
                    return 2;
                }

              }

              return -1;
        }
          
          for(int i(0);i<b.size();i++){
               string temp=b.substr(i,a.size());
               if(temp==a){
                   st=i;
                   end=i+a.length()-1;
                   cnt++;
                   break;
               }
          }

          queue<char>qu;
          for(int i(0);i<a.size();i++){
                qu.push(a[i]);
          }

          queue<char>temp;
          temp=qu;

          for(int i(end+1);i<b.size();i++){
                 if(temp.front()!=b[i]){
                      return -1;
                 }
                 temp.pop();
                 

                if(temp.empty()){
                     temp=qu;
                }
          }

           while(!qu.empty()){
              qu.pop();
           }

          for(int i(a.size()-1);i>=0;i--){
                qu.push(a[i]);
          }

          temp=qu;


         for(int i(st-1);i>=0;i--){

                        //    cout<<temp.front()<<endl;
                
                 if(temp.front()!=b[i]){
                      return -1;
                 }

                 temp.pop();

                if(temp.empty()){
                     temp=qu;
                }
              
          }

          

          if(st==-1){
             return -1;
          }

          int backl=st;

          
          cnt+=((backl)/a.size());
         

          if(backl%a.size()){
              cnt++;
          }

          int frl=(b.size()-end-1);
          cnt+=((frl)/a.size());

          if(frl%a.size()){
             cnt++;
          }


         return cnt;

    }
};
166198