Skip to content

Instantly share code, notes, and snippets.

@galacticmonkeys
Created April 10, 2016 07:05
Show Gist options
  • Save galacticmonkeys/e52bffadabcd9511b118355c1ae47f2d to your computer and use it in GitHub Desktop.
Save galacticmonkeys/e52bffadabcd9511b118355c1ae47f2d to your computer and use it in GitHub Desktop.
If a=1, b=2, c=3,....z=26. Given a string, find all possible codes that string can generate. Give a count as well as print the strings. For example: Input: "1123". You need to general all valid alphabet codes from this string.
// js implementation
function combination(stringOfNum) {
if (stringOfNum.length == 1) {
return 1;
} else {
var res = [];
res[0] = 1;
for (var n = 1; n < stringOfNum.length; n++) {
var condition = (parseInt(stringOfNum.slice(-2)) <= 26) ? 1 : 0;
res[n] = res[n-1] + condition;
}
return res[stringOfNum.length - 1];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment