Last active
December 5, 2015 07:02
-
-
Save jinwolf/17a8294202aac00b68c1 to your computer and use it in GitHub Desktop.
JavaScript permutation
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
| // a + permute(b, c, d); | |
| // a + b + permute(c, d); | |
| // a + c + permute(b, d); | |
| // a + d + permute(b, c) | |
| function permute(str) { | |
| var used = new Array(4); | |
| var list = str.split(''); | |
| var out = []; | |
| doPermute(used, list, 0, out); | |
| } | |
| function doPermute(used, list, start, out) { | |
| if (start == list.length) { | |
| return console.log(out.join('')); | |
| } | |
| for (var i=0; i < list.length; i++) { | |
| if (used[i] === true) { | |
| continue; | |
| } | |
| used[i] = true; | |
| out.push(list[i]); | |
| doPermute(used, list, start + 1, out); | |
| used[i] = false; | |
| out.pop(); | |
| } | |
| } | |
| //permute(list); | |
| permute('abcd'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment