VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-possible-decoding-of-a-given-digit-sequence-with-hidden-characters/

⇱ Count possible decoding of a given digit sequence with hidden characters - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count possible decoding of a given digit sequence with hidden characters

Last Updated : 23 Jul, 2025

Given a string S containing digits and character '*' i.e. hidden character, the task is to find the number of ways to decode this hidden character of the given string.

Since the answer may be very large, return it modulo 109+7.

A string containing letters from A-Z can be encoded into numbers using the following mapping:

'A' -> "1" 
'B' -> "2" 
'C' -> "3" 
'D' -> "4" 
... 
... 
'Z' -> "26" 
Note: Characters including 0 are not included in the problem like (J ? 10).

Examples: 

Input: s = "*"
Output: 9
Explanation: The encoded message can represent any of the encoded messages "1", "2", "3", "4", "5", "6", "7", "8", or "9".
Each of these can be decoded to the strings "A", "B", "C", "D", "E", "F", "G", "H", and "I" respectively.
Hence, there are a total of 9 ways to decode "*".

Input: s = "1*"
Output: 18
Explanation: The encoded message can represent any of the encoded messages "11", "12", "13", "14", "15", "16", "17", "18", or "19".
Each of these encoded messages have 2 ways to be decoded (e.g. "11" can be decoded to "AA" or "K").
Hence, there are a total of 9 × 2 = 18 ways to decode "1*". 

Approach: 

This problem can be solved by observing that any constant number can be either decoded in a character which is a single-digit number or it can be decoded into a double-digit number if (i-1)th character is '1' or (i-1)th character is '2' and ith character is between 1 and 6. Therefore, the current state depends on the previous state, and dynamic programming can be used to solve the problem. Follow the steps below to solve the problem: 

1. Let dp[i] represent the number of ways to decode the string characters from 0 to i.

2. If the ith character is '*' :

  • dp[i] = dp[i-1]*9 considering '*' can be 1 to 9, and it is considered alone as a character.
  • Now, if the i and i-1 characters are combined, then,
    • If (i-1)th character is '*' then the two “**” together can form 15 possible characters(like 13 will form character 'M'), so we add 15×dp[i-2] to dp[i].
    • If (i-1)th character is '1' then dp[i] = dp[i] + 9×dp[i-2] because the possible characters that can be decoded will be 11 to 19(K to S).
    • If (i-1)th character is '2' then dp[i] = dp[i] + 6×dp[i-2] as it can take value from 21 to 26.

3. If the ith character is not '*':

  • dp[i] = dp[i] + dp[i-1] considering ith character alone as a number.
  • Now, if it is possible to combine (i-1)th character and ith character together then add dp[i-2] to dp[i].

Below is the implementation of the above approach: 


Output
2

Time complexity: O(n)
Auxiliary Space: O(n)

Further optimization of space 

If the above code is observed carefully, it is observed that the value of dp[i] is found using dp[i-1] and dp[i-2]. So to optimize the space further, instead of creating an array of dp of length N, we can use three variables - second(stores the value of dp[i]), first(stores the value of dp[i-2]), and temp(stores the value of dp[i-1]). So after finding the value of the second(dp[i]), modify first = temp and temp = second and then calculate the value again of second(dp[i]) using the variable first and temp


Output
9

Time complexity: O(n)
Auxiliary Space: O(1)


 

Comment