Last active
March 10, 2017 18:55
-
-
Save jasonwaters/5c818d4455b512058586a3f0fd0be5aa to your computer and use it in GitHub Desktop.
String Permutations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function permute(arr, permutation=[], permutations=[]) { | |
| if(arr.length === 0) { | |
| permutations.push(permutation); | |
| }else { | |
| arr.forEach(item => { | |
| permute(arr.filter(value => value != item), permutation.concat(item), permutations); | |
| }); | |
| } | |
| return permutations; | |
| } | |
| console.log(permute('abc'.split(''))); | |
| //abc | |
| //acb | |
| //bac | |
| //bca | |
| //cab | |
| //cba |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment