Skip to content

Commit ae02c01

Browse files
authored
Create permutations_with_recursion.js
1 parent d9e6b50 commit ae02c01

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

permutations_with_recursion.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
3+
Given a string, return list of all possible permutations
4+
Use recursion
5+
6+
*/
7+
8+
function permutations(str){
9+
10+
let perms = [];
11+
12+
if (str.length < 2) return str;
13+
14+
for (let i=0; i<str.length; i++){
15+
16+
const currentChar = str[i];
17+
const remainingStr = str.slice(0, i) + str.slice(i+1, str.length);
18+
19+
for (let subPerm of permutations(remainingStr)){
20+
perms.push(currentChar + subPerm);
21+
}
22+
}
23+
24+
return perms;
25+
26+
}
27+
28+
permutations('abc');
29+
30+
31+

0 commit comments

Comments
 (0)