Last active
January 6, 2019 12:33
-
-
Save amalic/5e66e42a9f1f1fe58479b8d32e568090 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: