-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
41 lines (37 loc) · 899 Bytes
/
solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* @param {string} A
* @param {string} B
* @return {number}
*/
var repeatedStringMatch = function(A, B) {
let lengthA = A.length,
lengthB = B.length,
i = j = 0,
next = []
while (i < lengthB) {
if (i === 0 || (B[i] !== B[j] && j === 0)) {
next.push(0)
} else if (B[i] === B[j]) {
next.push(++j)
} else if (B[i] !== B[j] && j > 0) {
j = next[j - 1]
continue
}
i++
}
i = j = 0
while (j < lengthB) {
if (A[i % lengthA] === B[j]) {
i++
j++
} else if (j === 0) {
i++
} else if (j > 0) {
j = next[j - 1]
}
if (i >= lengthA + lengthB) {
return -1
}
}
return i % lengthA > 0 ? parseInt(i / lengthA) + 1 : parseInt(i / lengthA)
};