Skip to content

Instantly share code, notes, and snippets.

@cosmicbuffalo
Created May 24, 2017 03:51
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 cosmicbuffalo/c9d30bd2a69d08bef24eae3c6b7d9da0 to your computer and use it in GitHub Desktop.
Save cosmicbuffalo/c9d30bd2a69d08bef24eae3c6b7d9da0 to your computer and use it in GitHub Desktop.
A recursive algorithm to get all the possibilities of a binary string after filling in placeholders
function binaryCombos(str){
var placeholder = "_";
var result = [];
if (!str.includes(placeholder)){
result.push(str);
return result;
}
for(var x = 0; x < str.length; x += 1){
if (str[x] == placeholder){
result = result.concat(binaryCombos(str.slice(0,x) + "0" + str.slice(x+1)));
result = result.concat(binaryCombos(str.slice(0,x) + "1" + str.slice(x+1)));
break;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment