Best Explanation. Beats 100%. Building Intuition from scratch.
Intuition and Approach\n Describe your first thoughts on how to solve this problem. \nFor some cases s2 might be a subsequence of s1 so finding the no of times 
# Intuition and Approach\n<!-- Describe your first thoughts on how to solve this problem. -->\nFor some cases s2 might be a subsequence of s1 so finding the no of times s2 comes in s1,n1 is a direct method. for eg: **s1 = "acb", n1 = 4, s2 = "ab", n2 = 2**\n\n```\nstr1= acbacbacbacb\ns2= ab (note: s2 not str2)\ns2 comes 4 time in str1\n\nans= 4/n2\nans= 2\n```\nBut s1*n1 can be a very big string at times and iterating over the entire string might not be feasible. so if s2 is a subsequence of s1, we can use\n\n```\ns1= acb\ns2= ab \ns2 comes once in s1\nso in 4*s1, s2 will come 4 times\n\nans= 4/n2\nans= 2\n\nOR\n\ns1= acbacb\ns2= ab \ns2 comes twice in s1\nso in 4*s1, s2 will come 8 times\n\nans= 8/n2\nans= 4\n```\n\nbut in many case it might be possible that s2 does not fit in one iteration of s1\neg:\n```\ns1= acb n1=4\ns2= abb n2=2\n\nhere s2 comes in s1*2\ns1*2 ====> acbacb\ns2= abb\n\nstr2 will come only once in str1\nstr1= acbacbacbacb\nstr2= abbabb\n```\nwe can try to detect a cycle by checking when will we get our first 2 iterations.\n\nconsidering index iterates over s2(will understand in code)\ns1Count= no of time we have iterated over s1\ns2Count no of time we have iterated over s2\n```\ns1= acb n1=16\ns2= abb n2=2\n\nhere s2 comes first time in s1*2\ns1*2 ==> acbacb\ns2= abb\nand we reach at index zero again after finding complete s2. \nthis is to find s2 second or more times in s1*n1 i.e str1 \n```\nso,\ns1Count= 2 \ns2Count=1 since we completely iterated over this string only once\n\nnow we will try to find s2 again in remaining n1-s1 Counts\nwhen we find s1 2nd time:\n```\ns1Count= 4\ns2Count= 2\n```\n\nsince we found s2 twice, now we can tell the length of the cycle, i.e the times we will have to iterate over s1 to find complete s2.\n\n```\nfirstS1= 2\nsecondS1= 4\ntherefore, cycleLength= secondS1- firstS1= 2\n```\n\nwe also need to find the no of remaining cycles:\n```\ncycleCount= (n1- firstS1)/cycleLength;\n```\n\nnow no of times we can find s2 in remaining cycles is cycleCount multiplied by no of times we found s2 in this cycle, i.e secondS2- firstS2\n```\nno of s2 we will find=  cycleCount*(secondS2- firstS2);\n```\n\n# Complexity\n- Time complexity: Worst Case-> O(n1 * len1)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: worst case-> O(len2) to store all the indexes of s2\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int getMaxRepetitions(string s1, int n1, string s2, int n2) {\n        int len1 = s1.size();  // Length of s1\n        int len2 = s2.size();  // Length of s2\n        \n        // A map to store the index of s2 and corresponding (s1_count, s2_count)\n        unordered_map<int, pair<int, int>> memo;\n        \n        int s1Count = 0;  // Number of times s1 has been concatenated\n        int s2Count = 0;  // Number of times s2 has been fully matched\n        int index = 0;    // Current index in s2\n        \n        while (s1Count < n1) {  // Loop until we use s1 n1 times\n            for (char c : s1) {  // Traverse through each character in s1\n                if (c == s2[index]) {  // If characters match\n                    index++;\n                    if (index == len2) {  // If we have matched the entire s2\n                        s2Count++;\n                        index = 0;  // Reset index for the next round\n                    }\n                }\n            }\n            s1Count++;  // Increment the count of s1 used\n            \n            // Check if we have seen this index of s2 before\n            if (memo.find(index) != memo.end()) {\n                auto& prev = memo[index];\n                int prev_s1Count = prev.first;\n                int prev_s2Count = prev.second;\n\n                // Calculate the cycle length and the number of cycles\n                int cycleLen = s1Count - prev_s1Count;\n                int cycleCount = (n1 - prev_s1Count) / cycleLen;\n\n                // Fast forward through the cycles\n                s1Count = prev_s1Count + cycleCount * cycleLen;\n                s2Count = prev_s2Count + cycleCount * (s2Count - prev_s2Count);\n            }\n            \n            // Store the current state in the map\n            memo[index] = {s1Count, s2Count};\n        }\n        \n        // Calculate the final result by dividing s2Count by n2\n        return s2Count / n2;\n    }\n};\n\n```\n\n![image.png](https://assets.leetcode.com/users/images/6b56d7af-3d34-42ac-91de-962f918e9c2b_1720689646.5977323.png)\n\n
['C++']