decoding a string

🏠

Can't remember the exact name of this problem, but essentially involves counting the number of different ways a string of numbers representing letters of the alphabet can be interpreted.

 1 S = "26157"
 2 data = {'ways':-1}
 3 
 4 def count_number_of_ways(s):
 5     data['ways'] += 1
 6     if len(s) >= 2:
 7         count_number_of_ways(s[1:])
 8     if len(s) >= 2 and int(s[0:2]) <= 26:
 9         count_number_of_ways(s[2:])
10     return data['ways']
11 
12 ways = count_number_of_ways(S)
13 print(ways)

Output:

 1 2 --> 6157
 2 6 --> 157
 3 1 --> 57
 4 5 --> 7
 5 15 --> 7
 6 26 --> 157
 7 1 --> 57
 8 5 --> 7
 9 15 --> 7
10 9