Created
November 7, 2015 10:30
-
-
Save alexislagante/60accba5dcd7b410d186 to your computer and use it in GitHub Desktop.
Get all possible combinations of a pattern (? is replacable by an alphabet)
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 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