Last active
December 4, 2019 16:11
-
-
Save rishi93/6e6f0f70d4b90d74c91e477d65e469c3 to your computer and use it in GitHub Desktop.
Daily Coding Problem - 7
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
""" | |
This problem was asked by Facebook. | |
Given the mapping a = 1, b = 2, ... z = 26, and an encoded message, count the number of ways it can be decoded. | |
For example, the message '111' would give 3, since it could be decoded as 'aaa', 'ka', and 'ak'. | |
You can assume that the messages are decodable. For example, '001' is not allowed. | |
""" | |
def count(string): | |
n = len(string) | |
return _count(string, n, 0) | |
def _count(string, n, i): | |
if i == n: | |
return 1 | |
if i == n-1: | |
return 1 | |
else: | |
if int(string[i:i+2]) in range(10,26+1): | |
return _count(string, n, i+1) + _count(string, n, i+2) | |
else: | |
return _count(string, n, i+1) | |
print(count("111")) # 3 | |
print(count("2222")) # 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment