When using a simple encryption method in which the letters are replaced by the indexing numbers of the alphabet, there are multiple ways of decrypting them, ex. ABOR is 121518 but 121518 could also be AYEAH or LAER.
Well I need an algorithm to calculate how many possible ways there are for a given number to decrypt the message via the described method (ex 1225 has 5 – ABBE, AVE, ABY, LBE, LY ).
Help me if you want to.
You can do it recursively.
The total number of ways of encoding the first n digits is (the number of ways of encoding the first n-1 digits if the last digit is 1 <= d <= 9) + (the number of ways of encoding the first n-2 digits if the last two digits are 10 <= dd <=26).
Cache results or use dynamic programming to prevent exponential explosion. The technique is very similar to calculating Fibonacci numbers.
Here’s how you could do it in Python, to demonstrate the principle:
You could do it in a similar way in C++, but as this seems to be a homework / learning exercise, I hope you don’t mind that I’ll leave you to work out the details in C++.