Skip to content

Instantly share code, notes, and snippets.

@antsmartian
Last active February 9, 2017 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antsmartian/4156d154d0fcacb584bd8a4c072183cc to your computer and use it in GitHub Desktop.
Save antsmartian/4156d154d0fcacb584bd8a4c072183cc to your computer and use it in GitHub Desktop.
combinations and permutations
function getCombinations(totalChars) {
var result = []
var fn = function(prefix, chars) {
for(let i=0;i<chars.length;i++) {
result.push(prefix + chars[i])
fn(prefix + chars[i],chars.slice(i+1))
}
}
fn('',totalChars);
return result
}
console.log( getCombinations("abc".split("")) )
function getPermutations(str) {
var result = [];
var fn = function(prefix,str) {
for(var i=0;i<str.length;i++) {
result.push(prefix )
permutation(prefix + str.charAt(i), str.substring(0,i) + str.substring(i+1,str.length) )
}
}
fn("",str);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment