Created
December 29, 2011 04:00
-
-
Save mlhaufe/1531772 to your computer and use it in GitHub Desktop.
JavaScript 1.5 Permutations
This file contains 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
var perm = (function(){ | |
function perm(xs, ys, o){ | |
if(ys.length){ | |
for(var i=0,v;(v=xs+ys.charAt(i)),i<ys.length;i++){ | |
o[v] = 1; | |
perm(v, ys.slice(0,i) + ys.slice(i + 1),o) | |
} | |
} | |
} | |
return function(ys){ | |
var o = {}, a = [] | |
perm('',ys, o) | |
for(var i in o) | |
a[a.length] = i; | |
return a; | |
} | |
})() | |
var p = perm('abcdef') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment