-
Notifications
You must be signed in to change notification settings - Fork 1
/
686.js
45 lines (37 loc) · 905 Bytes
/
686.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
42
43
44
45
function repeatedStringMatch(a, b) {
if (b === '') {
return 0;
}
let canMatch = true;
for (let char of b) {
if (a.indexOf(char) < 0) {
canMatch = false;
}
}
if (a.length > b.length && !judgeSubstr(a + a, b)) {
canMatch = false
}
console.log('canMatch is', canMatch)
if (!canMatch) {
return -1;
}
let count = 1;
let basic = a;
while (!judgeSubstr(basic, b)) {
basic += a;
++count;
}
return count;
function judgeSubstr(str, substr) {
if (str.length < substr.length) {
return false;
}
for (let i = 0; i < str.length; i++) {
if (str.slice(i,i+substr.length) === substr) {
return true
}
}
return false;
}
}
console.log(repeatedStringMatch('aaaaaaaaaaaaaaaaaaaaaab', 'ba'));