-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall-inclusive.js
More file actions
48 lines (45 loc) · 1.53 KB
/
all-inclusive.js
File metadata and controls
48 lines (45 loc) · 1.53 KB
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
46
47
48
/*
Title:
All Inclusive?
Description:
Input:
- a string strng
- an array of strings arr
Output of function contain_all_rots(strng, arr) (or containAllRots or contain-all-rots):
- a boolean true if all rotations of strng are included in arr (C returns 1)
- false otherwise (C returns 0)
Examples:
contain_all_rots(
"bsjq", ["bsjq", "qbsj", "sjqb", "twZNsslC", "jqbs"]) -> true
contain_all_rots(
"Ajylvpy", ["Ajylvpy", "ylvpyAj", "jylvpyA", "lvpyAjy", "pyAjylv", "vpyAjyl", "ipywee"]) -> false)
Notes:
Though not correct in a mathematical sense
- we will consider that there are no rotations of strng == ""
- and for any array arr: contain_all_rots("", arr) --> true
Ref: https://en.wikipedia.org/wiki/String_(computer_science)#Rotations
Kata Link:
https://www.codewars.com/kata/all-inclusive
Discuss Link:
https://www.codewars.com/kata/all-inclusive/discuss
Solutions Link:
https://www.codewars.com/kata/all-inclusive/solutions
*/
// Long Solution
/*
const containAllRots = (strng, arr) => {
if (!strng) return true
const allRotations = strng
.split('')
.map((_, index) => `${strng.slice(index)}${strng.slice(0, index)}`)
const allRotationsExists = allRotations.every(el => arr.includes(el))
return allRotationsExists
}
*/
// Short Solution
const containAllRots = (strng, arr) =>
[...strng]
.map((_, index) => `${strng.slice(index)}${strng.slice(0, index)}`)
.every(el => arr.includes(el))
// Function Export
module.exports = containAllRots