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