Skip to content

Instantly share code, notes, and snippets.

@alexislagante
Created November 7, 2015 10:30
Show Gist options
  • Save alexislagante/60accba5dcd7b410d186 to your computer and use it in GitHub Desktop.
Save alexislagante/60accba5dcd7b410d186 to your computer and use it in GitHub Desktop.
Get all possible combinations of a pattern (? is replacable by an alphabet)
function pattern(str, alphabet) {
alphabet = alphabet || ["0", "1"];
var chars = str.split("");
var result = [];
chars.forEach(function(char) {
if (result.length == 0) {
result = [""];
}
var newResult = [];
result.forEach(function(item){
if (char === '?') {
alphabet.forEach(function(letter) {
newResult.push(item+letter);
});
} else {
newResult.push(item+char);
}
});
result = newResult;
});
return result;
}
pattern("01?01??");
pattern("?010?");
pattern("???",["a","b","c"]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment