Skip to content

Commit 7eee706

Browse files
authored
Create permutations.js
1 parent f87609f commit 7eee706

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

permutations.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const permute = (rest) => {
2+
if(!rest.length) {
3+
permutations.push(permutation.join(""));
4+
return;
5+
}
6+
for (let i = 0; i < rest.length; i++) {
7+
permutation = permutation.concat(rest[i]);
8+
permute(rest.split("").filter(c => c !== rest[i]).join(""));
9+
permutation = permutation.slice(0, permutation.length - 1);
10+
}
11+
}
12+
13+
let permutations = [];
14+
let permutation = [];
15+
const input = "abc";
16+
permute(input);
17+
console.log(permutations);
18+

0 commit comments

Comments
 (0)