Skip to content

Instantly share code, notes, and snippets.

@santhalakshminarayana
Created September 4, 2019 18:08
Show Gist options
  • Save santhalakshminarayana/62c120bbe9f9f06a6f14ae700df02a3b to your computer and use it in GitHub Desktop.
Save santhalakshminarayana/62c120bbe9f9f06a6f14ae700df02a3b to your computer and use it in GitHub Desktop.
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'.
'''
def decode_msg(msg):
n=len(msg)
if n==0 or msg=='0' or msg[0]=='0':
return 0
n=len(msg)
count=[0]*(n+1)
count[0]=1
count[1]=1
for i in range(2,n+1):
count[i] = 0
if(msg[i-1] > '0'):
count[i]=count[i-1]
if(msg[i-2]=='1' or (msg[i-2]=='2' and msg[i-1] < '7') ):
count[i]+=count[i-2]
return count[n]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment