We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 01d2ec5 commit 1eebb5dCopy full SHA for 1eebb5d
16_permutations/solution/permutations-solution.js
@@ -1,6 +1,19 @@
1
-const permutations = function() {
2
- // Replace this comment with the solution code
+const permutations = function (original, currentPermutations) {
+ if (original.length === 1) return original;
3
+
4
+ const perms = currentPermutations
5
+ ? currentPermutations
6
+ : original.map((el) => [el]);
7
8
+ const newPerms = [];
9
+ perms.forEach((el) => {
10
+ const missing = original.filter((item) => !el.includes(item));
11
+ missing.forEach((itemMissing) => newPerms.push([...el, itemMissing]));
12
+ });
13
14
+ if (newPerms.every((el) => el.length === original.length)) return newPerms;
15
+ return permutations(original, newPerms);
16
};
-
17
18
// Do not edit below this line
19
module.exports = permutations;
0 commit comments