-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSolution.js
More file actions
45 lines (41 loc) 路 1014 Bytes
/
Copy pathSolution.js
File metadata and controls
45 lines (41 loc) 路 1014 Bytes
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
42
43
44
45
/**
* @param {string} A
* @param {string} B
* @return {number}
*/
// var repeatedStringMatch = function(A, B) {
// var ans = 0, lengthA = A.length, lengthB = B.length - 1, i, j, check;
// for (i = 0; i < lengthA; i++) {
// j = 0;
// check = true;
// if (A[i] !== B[j]) continue;
// while(j < lengthB) {
// j++;
// if (A[(i + j) % lengthA] !== B[j]) {
// check = false;
// break;
// }
// }
// if (check) {
// ans = Math.ceil((i + j + 1) / lengthA);
// break;
// }
// }
// return ans === 0 ? -1 : ans;
// };
/**
* @param {string} A
* @param {string} B
* @return {number}
*/
var repeatedStringMatch = function(A, B) {
var length = B.length, i, q = 0, S = '';
while(S.length < length) {
S += A;
q++;
}
if (S.indexOf(B) > -1) return q;
S += A;
if (S.indexOf(B) > -1) return q + 1;
return -1;
};