java solution | 4ms runtime | with brief explanation
Explanation\n\n. For each repetition of s1, count the number of times we see s2\n. Store the seen count for each repetition of s1\n. Store the index of s2 where
# Explanation\n\n. For each repetition of s1, count the number of times we see s2\n. Store the seen count for each repetition of s1\n. Store the index of s2 where we stopped after each repetition of s1\n. For each repetition of s1, check if we\'ve seen this index of s2 before\n. If we have, then we\'ve found a repeating pattern\n. The number of times we see s2 in the pattern is (count[k] - count[start])\n. The number of times we see s2 outside the pattern is (count[start + (n1 - start) % (k - start)] - count[start])\n. The number of times we see s2 in total is (prefixCount + patternCount + suffixCount)\n. The number of times we see s2 in total divided by n2 is the answer\n\n# Code\n```\n// Count the repetitions\n// LeetCode: https://leetcode.com/problems/count-the-repetitions/\n\nclass Solution {\n    public int getMaxRepetitions(String s1, int n1, String s2, int n2) {\n        int[] next = new int[s2.length() + 1];\n        int[] count = new int[s2.length() + 1];\n        int j = 0, cnt = 0;\n        for (int k = 1; k <= n1; k++) {\n            for (int i = 0; i < s1.length(); i++) {\n                if (s1.charAt(i) == s2.charAt(j)) {\n                    j++;\n                    if (j == s2.length()) {\n                        j = 0;\n                        cnt++;\n                    }\n                }\n            }\n            count[k] = cnt;\n            next[k] = j;\n            for (int start = 0; start < k; start++) {\n                if (next[start] == j) {\n                    int prefixCount = count[start];\n                    int patternCount = (count[k] - count[start]) * ((n1 - start) / (k - start));\n                    int suffixCount = count[start + (n1 - start) % (k - start)] - count[start];\n                    return (prefixCount + patternCount + suffixCount) / n2;\n                }\n            }\n        }\n        return count[n1] / n2;\n    }\n}\n```\n```C++[]\nclass Solution {\npublic:\n    int getMaxRepetitions(string s1, int n1, string s2, int n2) {\n        vector<int> rapport(102,-1);\n        vector<int> rest(102,-1);\n        int b=-1;int posRest=0;int rap=0;\n        int last=-1;\n        rapport[0]=rest[0]=0;\n        for(int i=1;i<=s2.size()+1;i++){\n            int j;\n            for(j=0;j<s1.size();j++){\n                if(s2[posRest]==s1[j]){\n                    posRest++;\n                    if(posRest==s2.size()){\n                        rap++;\n                        posRest=0;\n                    }\n                }\n            }\n            for(int k=0;k<i;k++){\n                if(posRest==rest[k]){b=k;last=i;break;}\n            }\n            rapport[i]=rap;rest[i]=posRest;\n            if(b>=0)break;\n        }\n        int interval=last-b;\n        if(b>=n1)return rapport[n1]/n2;\n        return ((n1-b)/interval*(rapport[last]-rapport[b])+rapport[(n1-b)%interval+b])/n2;  }\n};\n```\n```python3 []\nclass Solution(object):\n    def getMaxRepetitions(self, s1, n1, s2, n2):\n        start = {}\n        s1_round, s2_round, s2_idx = 0, 0, 0\n        while s1_round < n1:\n            s1_round += 1\n            for ch in s1:\n                if ch == s2[s2_idx]:\n                    s2_idx += 1\n                    if s2_idx == len(s2):\n                        s2_round += 1\n                        s2_idx = 0\n            if s2_idx in start:\n                prev_s1_round, prev_s2_round = start[s2_idx]\n                circle_s1_round, circle_s2_round = s1_round - prev_s1_round, s2_round - prev_s2_round\n                res = (n1 - prev_s1_round)\n                left_s1_round = (n1 - prev_s1_round) % circle_s1_round + prev_s1_round\n                for key in start:\n                    val = start[key]\n                    if val[0] == left_s1_round:\n                        res += val[1]\n                        break\n                return res \n            else:\n                start[s2_idx] = (s1_round, s2_round)\n        return s2_round\n```\n\n\n
['C++', 'Java', 'Python3']