-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
47 lines (32 loc) · 1.4 KB
/
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
42
43
44
45
46
47
const isSpliceJoin = (s, l, v) => {
if (s.length !== v.length) return false;
const getSpliceJoinStrings = (str, length) => {
const spliceJoinStrings = [];
for (let i = 0; i < str.length; i += length) {
spliceJoinStrings.push(str.substring(i, i + length));
}
return spliceJoinStrings;
}
const spliceStrings = getSpliceJoinStrings(s, parseInt(l));
const getSpliceJoinPermutationStrings = (spliceJoinStrings, start, lastStr) => {
const stringPermutation = [];
const permute = (arr, memo = []) => {
let cur;
for (let i = 0; i < arr.length; i++) {
if (memo.length === 0 && (arr[i] !== start && arr[i] !== lastStr)) continue;
cur = arr.splice(i, 1);
if (arr.length === 0) {
stringPermutation.push(memo.concat(cur));
}
permute(arr, memo.concat(cur));
arr.splice(i, 0, cur[0]);
}
return stringPermutation;
}
return permute(spliceJoinStrings).map(str => str.join(''));
}
const permutatedSpliceJoinStrings = getSpliceJoinPermutationStrings(spliceStrings, v.substring(0, l), spliceStrings[spliceStrings.length - 1]);
return permutatedSpliceJoinStrings.indexOf(v) !== -1;
}
const args = process.argv.slice(2);
console.log(isSpliceJoin(args[0], args[1], args[2]));