Search algorithm for possible ways of simple, multiple encryption with the alphabet

When using a simple encryption method in which letters are replaced by alphabetical index numbers, there are several ways to decrypt them, for example. ABOR is 121518, but 121518 can also be AYEAH or LAER.

Well, I need an algorithm to calculate the number of possible ways for a given number to decrypt a message using the described method (ex 1225 has 5 - ABBE, AVE, ABY, LBE, LY).

Help me if you want.

+3
source share
1 answer

You can do this recursively.

n ( n-1 , 1 <= d <= 9) + ( n-2 , 10 <= dd <= 26).

. .

Python, :

# s is of form [1-9][0-9]*
s = '121518'
a=1 # Cache of f(i - 2)
b=1 # Cache of f(i - 1)
for i in range(1, len(s)):
   a, b = b, a * (10 <= int(s[i - 1: i + 1]) <= 26) + b * (s[i] != '0')
print b

++, , , /, , , ++.

+3

Source: https://habr.com/ru/post/1732599/


All Articles