Skip to content

Instantly share code, notes, and snippets.

@AntonisFK
Created May 18, 2016 08:10
Show Gist options
  • Save AntonisFK/f2a3fe436172422b9bb34f3d4bf1daec to your computer and use it in GitHub Desktop.
Save AntonisFK/f2a3fe436172422b9bb34f3d4bf1daec to your computer and use it in GitHub Desktop.
function permute(string) {
function recur(string, prefix) {
if (string.length === 0) {
console.log(prefix , "from if ")
return [prefix];
} else {
var out = [];
for (var i = 0; i < string.length; i++) {
var pre = string.substring(0, i);
var post = string.substring(i + 1);
console.log("pre:", pre, "+", "post:", post, "string[i]:", string[i],"+ prefix:", prefix)
console.log(out)
out = out.concat(recur(pre + post, string[i] + prefix));
}
console.log(out, ":out of loop")
return out;
}
}
console.log('string:', string)
var distinct = {};
recur(string, "").forEach(function(result) {
console.log('result', result)
distinct[result] = true;
});
return Object.keys(distinct);
}
permute('cat')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment