Skip to content

Instantly share code, notes, and snippets.

@rbcmgs
Created September 15, 2022 15:06
Show Gist options
  • Select an option

  • Save rbcmgs/51b74611f3138cbb61e630461df5c333 to your computer and use it in GitHub Desktop.

Select an option

Save rbcmgs/51b74611f3138cbb61e630461df5c333 to your computer and use it in GitHub Desktop.
javascript find all character combinations
function combinations(str) {
var fn = function(active, rest, a) {
if (!active && !rest)
return;
if (!rest) {
a.push(active);
} else {
fn(active + rest[0], rest.slice(1), a);
fn(active, rest.slice(1), a);
}
return a;
}
return fn("", str, []);
}
console.log(combinations("abcde"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment