Skip to content

Commit e605c8e

Browse files
authored
Create 466-count-the-repetitions.js
1 parent 9e7727f commit e605c8e

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

466-count-the-repetitions.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {string} s1
3+
* @param {number} n1
4+
* @param {string} s2
5+
* @param {number} n2
6+
* @return {number}
7+
*/
8+
const getMaxRepetitions = function(s1, n1, s2, n2) {
9+
let j = 0,
10+
i,
11+
count = 0,
12+
perCycle = 0,
13+
firstEnd = -1,
14+
lastEnd = -1,
15+
nonMatch = 0
16+
for (i = 0; i < s1.length * n1; i++) {
17+
if (s2[j] === s1[i % s1.length]) {
18+
j++
19+
nonMatch = 0
20+
} else if (++nonMatch >= s1.length) break
21+
if (j === s2.length) {
22+
count++
23+
perCycle++
24+
j = 0
25+
if (lastEnd !== -1) continue
26+
else if (firstEnd === -1) {
27+
firstEnd = i
28+
perCycle = 0
29+
} else if ((i - firstEnd) % s1.length === 0) {
30+
let cycleLen = i - firstEnd
31+
let remainLen = s1.length * n1 - i - 1
32+
let cycles = Math.floor(remainLen / cycleLen)
33+
count += cycles * perCycle
34+
i += cycles * cycleLen
35+
}
36+
}
37+
}
38+
return Math.floor(count / n2)
39+
}

0 commit comments

Comments
 (0)