A few days back, Ninja encountered a string containing characters from ‘A’ to ‘Z’ which indicated a secret message. For security purposes he encoded each character of the string to its numeric value, that is, A = 1, B = 2, C = 3, till Z = 26 and combined them as a single sequence (SEQ) of digits of length N. Let's say the message was "LA", Ninja encoded it as 121 for L=12 and A=1.
Today, when he read the encoded secret message, he realised that he was not able to decode the original string. So, the Ninja is wondering in how many ways he can decode the numeric sequence to some valid string.
A valid string is a string with characters from A to Z and no other characters.
Example:Let the encoded sequence be 121,
The first way to decode 121 is:
1 = A
2 = B
1 = A
Thus, the decoded string will be ABA.
The second way to decode 121 is:
12 = L
1 = A
Thus, the decoded string will be LA.
The third way to decode 121 is:
1 = A
21 = U
Thus, the decoded string will be AU.
So, there will be 3 ways to decode the sequence 121 i.e. [(ABA), (LA), (AU)].
Note:
The input sequence will always have at least 1 possible way to decode.
As the answer can be large, return your answer modulo 10^9 + 7.
Follow Up:
Can you solve this using constant extra space?
The first line of input contains an integer T denoting the number of queries or test cases.
The first and only line of each test case contains a digit sequence.
Output format:
For each test case, print the number of ways to decode the given digit sequence in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
0 <= SEQ[i] <= 9
Time limit: 1 sec
2
121
1234
3
3
For test case 1: Refer to the example explained above.
For test case 2:
There will be 3 ways to decode it which are
The first way to decode this is:
1 = A
2 = B
3 = C
4 = D
Thus, the decoded string will be ABCD.
The second way to decode this is:
12 = L
3 = C
4 = D
Thus, the decoded string will be LCD.
The third way to decode this is:
1 = A
23 = W
4 = D
Thus, the decoded string will be AWD.
All the other ways to decode the sequence will lead to an invalid string.
2
1213
21031
5
1
The very first approach can be to try all possible decodings of the sequence and count the valid ones among them.
O(1.61 ^ N) per test case where N is the size of sequence and 1.61 is the Golden Ratio or Fibonacci Ratio.
In the worst case, there will be a Fibonacci(n) number of possible decodings. Thus, We are making 1.61 ^ N calls for the sequence.
O(N) per test case where N is the size of the sequence.
In the worst case, extra space will be used by the recursion stack as there can be a maximum of N number of recursive calls at a time.