Created
May 18, 2016 08:10
-
-
Save AntonisFK/f2a3fe436172422b9bb34f3d4bf1daec to your computer and use it in GitHub Desktop.
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
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