JavaScript 56ms O(s1.length * s2.length) Time and O(1) Space
Like other solutions, we also detect the cycle occurence, but repeat twice is enough. It passes the OJ in 56ms with O(1) space, let me know if there\'s somethin
Like other solutions, we also detect the cycle occurence, but repeat twice is enough. It passes the OJ in 56ms with O(1) space, let me know if there\'s something wrong.\n\n```javascript\nvar getMaxRepetitions = function(s1, n1, s2, n2) {\n    let j = 0, i, count = 0, perCycle = 0, firstEnd = -1, lastEnd = -1, nonMatch = 0;\n    for(i = 0; i < s1.length * n1; i++) {\n        if (s2[j] === s1[i % s1.length]) {\n            j++;\n            nonMatch = 0;\n        }\n        else if (++nonMatch >= s1.length) break;\n        if (j === s2.length) {\n            count++;\n            perCycle++;\n            j = 0;\n            if (lastEnd !== -1) continue;\n            else if (firstEnd === -1) {\n                firstEnd = i;\n                perCycle = 0;\n            }\n            else if ((i - firstEnd) % s1.length === 0) {\n                let cycleLen = i - firstEnd;\n                let remainLen = s1.length * n1 - i - 1;\n                let cycles = Math.floor(remainLen / cycleLen);\n                count += cycles * perCycle;\n                i += cycles * cycleLen;\n            }\n        }\n    }\n    return Math.floor(count / n2);\n};\n```\n\nRemoving the `nonMatch` check does not effect the time. Maybe there should be a test case like:\n\n```\naaaaaa 1000000\nb 1\n```
[]