Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0d3fea4

Browse files
Create permutations_with_recursion.js
1 parent b312d7b commit 0d3fea4

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /