Skip to content

Instantly share code, notes, and snippets.

@amalic
Last active January 6, 2019 12:33
Show Gist options
  • Save amalic/5e66e42a9f1f1fe58479b8d32e568090 to your computer and use it in GitHub Desktop.
Save amalic/5e66e42a9f1f1fe58479b8d32e568090 to your computer and use it in GitHub Desktop.
public class FacebookDecoder {
public static void main(String[] args) {
test("");
test("0");
test("01");
test("1001");
test("12");
test("26");
test("27");
test("101");
test("1010");
test("10210");
test("262");
test("272");
test("362");
test("1234567890");
}
static void test(String s) {
System.out.println("\"" + s + "\" -> " + numDecodePossibilities(s));
}
static int numDecodePossibilities(String s) {
int count = 0;
if(s.length()==0)
count = 1; // empty String translates to empty message
else {
int current, prev = 0;
for(int i=0; i<s.length(); i++) {
current = Integer.valueOf(String.valueOf(s.charAt(i)));
if(prev != 0) {
if(prev*10 + current <= 26)
count++;
} else if(current == 0)
return 0; // 2x consecutive '0' is not allowed, covers also strings starting with '0'
if(current != 0)
count++;
prev = current;
}
}
return count;
}
}
@amalic
Copy link
Author

amalic commented Jan 6, 2019

Output:

"" -> 1
"0" -> 0
"01" -> 0
"1001" -> 0
"12" -> 3
"26" -> 3
"27" -> 2
"101" -> 3
"1010" -> 4
"10210" -> 6
"262" -> 4
"272" -> 3
"362" -> 3
"1234567890" -> 11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment