Count The Repetitions LeetCode solution in JavaScript

1
/**
2
* @param {string} s1
3
* @param {number} n1
4
* @param {string} s2
5
* @param {number} n2
6
* @return {number}
7
*/
8
var getMaxRepetitions = function (s1, n1, s2, n2) {
9
const len1 = s1.length;
10
const len2 = s2.length;
11
let count1 = 0; // Number of times s2 can be formed from s1
12
let count2 = 0; // Number of times str2 = [s2, count2] can be formed from str1 = [s1, count1]
13
let i = 0; // Pointer for s1
14
let j = 0; // Pointer for s2
15
while (count1 < n1) {
16
if (s1[i] === s2[j]) {
17
j++;
18
if (j === len2) {
19
j = 0;
20
count2++;
21
}
22
}
23
i++;
24
if (i === len1) {
25
i = 0;
26
count1++;
27
}
28
}
29
return Math.floor(count2 / n2);
30
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0