-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path(7 kyu) All Inclusive.js
45 lines (40 loc) · 982 Bytes
/
(7 kyu) All Inclusive.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
// 1 Plain solution
function containAllRots(strng, arr) {
if (!strng.length) {
return true;
}
const rotations = [];
const strCopy = [...strng];
for (let i = 0; i < strng.length; i++) {
const l = strCopy.shift();
const candidate = [l, ...strCopy].join("");
strCopy.push(l);
if (!rotations.includes(candidate)) {
rotations.push(candidate);
}
}
const res = arr.filter((s) => rotations.includes(s));
return res.length === rotations.length;
}
// 2 Optimized solution
function containAllRots(strng, arr) {
function rotate(s) {
return s.substring(1) + s[0];
}
for (let i = 0, l = strng.length; i < l; ++i) {
if (arr.indexOf(strng) === -1) {
return false;
}
strng = rotate(strng);
}
return true;
}
// 3 Clever solution
function containAllRots(strng, arr) {
for (let i = 0; i < str.length; i++) {
if (arr.indexOf(str.slice(i) + str.slice(0, i)) === -1) {
return false;
}
}
return true;
}