<p>We define <code>str = [s, n]</code> as the string <code>str</code> which consists of the string <code>s</code> concatenated <code>n</code> times.</p>

<ul>
	<li>For example, <code>str == [&quot;abc&quot;, 3] ==&quot;abcabcabc&quot;</code>.</li>
</ul>

<p>We define that string <code>s1</code> can be obtained from string <code>s2</code> if we can remove some characters from <code>s2</code> such that it becomes <code>s1</code>.</p>

<ul>
	<li>For example, <code>s1 = &quot;abc&quot;</code> can be obtained from <code>s2 = &quot;ab<strong><u>dbe</u></strong>c&quot;</code> based on our definition by removing the bolded underlined characters.</li>
</ul>

<p>You are given two strings <code>s1</code> and <code>s2</code> and two integers <code>n1</code> and <code>n2</code>. You have the two strings <code>str1 = [s1, n1]</code> and <code>str2 = [s2, n2]</code>.</p>

<p>Return <em>the maximum integer </em><code>m</code><em> such that </em><code>str = [str2, m]</code><em> can be obtained from </em><code>str1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s1 = "acb", n1 = 4, s2 = "ab", n2 = 2
<strong>Output:</strong> 2
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s1 = "acb", n1 = 1, s2 = "acb", n2 = 1
<strong>Output:</strong> 1
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s1.length, s2.length &lt;= 100</code></li>
	<li><code>s1</code> and <code>s2</code> consist of lowercase English letters.</li>
	<li><code>1 &lt;= n1, n2 &lt;= 10<sup>6</sup></code></li>
</ul>

// class Solution {
// public: 
//     int getMaxRepetitions(string s1, int n1, string s2, int n2) {
//         int len1 = s1.size(), len2 = s2.size();
//        // vector<int> repeatCount(n1 + 1, 0), nextIndex(n1 + 1, 0);  
//        vector<int> repeatCount(len2+ 1, 0), nextIndex(len2 + 1, 0);  // iss tkh m pkka cycle
//         int j = 0, count = 0;
//         for (int k = 1; k <= n1; ++k) {
//             for (int i = 0; i < len1; ++i) {
//                 if (s1[i] == s2[j]) {
//                     j++;
//                     if (j == len2) {
//                         j = 0;
//                         count++;  
//                     }
//                 }
//             }
            
//             // Store the count of full s2 sequences matched and the next index to match in s2
//             repeatCount[k] = count;
//             nextIndex[k] = j;

//             // Check for a cycle  // waise tum soch rahe ye to n*n but nahi pehle hi cycle mil jayegi  len2 tkh m hi 
//             for (int start = 0; start < k; ++start) {
//                 if (nextIndex[start] == j) {  // A cycle is detected  // matches till  j in prev also
//                     int interval = k - start;  //  itne m ek cycle 
//                     int repeatInterval = (n1 - start) / interval; // total cycle count 
//                     int remaining = (n1 - start) % interval;  // count remains of s1  // start +rem is remains of s1
//                   // repeatInterval * (count - repeatCount[start]) cycle m kitne count  + (start+rem) m kitne s2
//                     return (repeatInterval * (count - repeatCount[start]) + repeatCount[start + remaining]) / n2;
//                 }
//             }
//         }

//         // No cycle detected, return result directly
//         return repeatCount[n1] / n2;
//     }
// };




class Solution {
public:
    int getMaxRepetitions(string s1, int n1, string s2, int n2) {
        int c1 = 0, c2 = 0, i = 0, j = 0, n = s1.size(), m = s2.size();
        while(c1 < n1) {
            if(s1[i] == s2[j]) j++;
            i++;
            if(i == n) i = 0, c1++;
            if(j == m) j = 0, c2++;
        }
        return c2 / n2;
    }
};
7600