Beats 96% users || Beginner Friendly || Fully explained
\n# Code\n\n/**\n * @param {string} s1\n * @param {number} n1\n * @param {string} s2\n * @param {number} n2\n * @return {number}\n */\nvar getMaxRepetitions = f
![image.png](https://assets.leetcode.com/users/images/78459a0f-9ea3-4747-a250-8a66e330e126_1712170195.9242198.png)\n# Code\n```\n/**\n * @param {string} s1\n * @param {number} n1\n * @param {string} s2\n * @param {number} n2\n * @return {number}\n */\nvar getMaxRepetitions = function(s1, n1, s2, n2) {\n    // Initialize variables to store the lengths of strings s1 and s2\n    let l1 = s1.length, l2 = s2.length;\n    // Initialize arrays to store next index to match in s2 and the count of matches\n    let next = new Array(l2 + 1).fill(0);\n    let count = new Array(l2 + 1).fill(0);\n    // Initialize variables to count the number of matches and track the current index in s2\n    let cnt = 0, p = 0;\n    \n    // Loop through each repetition of s1\n    for (let i = 0; i < n1; i++) {\n        // Loop through each character in s1\n        for (let j = 0; j < l1; j++) {\n            // Check if the current character in s1 matches the character in s2 at index p\n            if (s1.charAt(j) === s2.charAt(p)) {\n                p++;\n            }\n            // If the end of s2 is reached, increment the match count and reset p to 0\n            if (p === l2) {\n                cnt++;\n                p = 0;\n            }\n        }\n        // Store the match count and the next index to match in the arrays\n        count[i] = cnt;\n        next[i] = p;\n        // Check for repeated pattern in s2\n        for (let j = 0; j < i; j++) {\n            // If a repeating pattern is found\n            if (next[j] === p) {\n                // Calculate the count of complete patterns\n                let prev_count = count[j];\n                let pattern_count = (count[i] - count[j]) * Math.floor((n1 - j - 1) / (i - j));\n                let remain_count = count[j + (n1 - j - 1) % (i - j)] - count[j];\n                // Return the count of complete patterns divided by the number of repetitions of s2\n                return Math.floor((prev_count + pattern_count + remain_count) / n2);\n            }\n        }\n    }\n    // Return the total count of matches divided by the number of repetitions of s2\n    return Math.floor(count[n1 - 1] / n2);\n};\n\n```
['JavaScript']