Skip to content

Instantly share code, notes, and snippets.

@rishi93
Last active December 4, 2019 16:11
Show Gist options
  • Save rishi93/6e6f0f70d4b90d74c91e477d65e469c3 to your computer and use it in GitHub Desktop.
Save rishi93/6e6f0f70d4b90d74c91e477d65e469c3 to your computer and use it in GitHub Desktop.
Daily Coding Problem - 7
"""
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