Given an encoded string **str** consisting of digits and ***** which can be filled by any digit **1 – 9**, the task is to find the number of ways to decode that string into a sequence of alphabets **A-Z**.

**Note:** The input string contains number from 0-9 and character ‘*’ only.

**Examples:**

Input:str = “1*”

Output:18

Explanation:

Since * can be replaced by any value from (1-9),

The given string can be decoded as A[A-I] + [J-R] = 9 + 9 ways

Input:str = “12*3”

Output:28

**Naive Approach:** A simple solution is to solve the problem using recursion considering all the possible decodings of the string. Below is the recursion tree for the problem:

12*3 / \ 12*(3) 12(*3) / \ / \ 12(*)(3) 1(2*)(3) 1(2)(*3) "" / \ \ / 1(2)(*)(3) "" "" "" / ""

**Efficient Approach:** The idea is to solve the problem using Dynamic Programming using the optimal substructure for considering all the cases of the current and the previous digits of the string and their number of ways to decode the string.

**Definition of the DP State:** In this problem the denotes the number of ways to decode the string upto the index.

**Intial DP States:** Intially the value of the DP states are defined as below:

// Number of ways to decode // an empty string dp[0] = 1 // Number of ways to decode the // first character if (s[0] == '*') dp[1] = 9 // 9 ways else dp[1] = 1

**Optimal Sub-structure:** There are generally two ways to decode the current character of the string:

**Including the current character as single-digit to decode:**If the current character is used as single-digit then there can be generally two cases for the character to be:**Case 1:**If current character is equal to the , Then there are 9 possible ways to take any digit from [1-9] and decode it as any character from [A-Z].if (current == "*") dp[i] += 9 * dp[i-1]

**Case 2:**If current character is equal to any other digit from [0-9], Then the number of possible ways to decode is equal to the number of way to decode the string upto index.if (current != "*") dp[i] += dp[i-1]

**Including the current character as two-digits to decode:**If the current character is to be decoded as two-digits then there are two possible cases:**Case 1:**If the previous character is equal to the or , then there can be two more possible subcases which will depend upon the current character:**Case 1.1:**If the current character is equal to the , then total possible ways to decode are 9 if the previous character is 1, otherwise 6 if previous character is 2.**Case 1.2:**If the current character is less than equal to 6, Then the total number of possible way to decode the string will depend only on the number of way to decode upto the previous character. That is

**Case 2:**If the previous character is , then there can be two more possible subcases which will depend upon the current character:**Case 2.1:**If the current character is also , Then the total number of cases will be , because the single digits of decoding ways of previous character must be already included.**Case 2.2:**If the current character is less than 6, Then the total number of ways will be , because then the number of ways to choose the digits of the first character is 2. That is [1, 2].**Case 2.3:**If the current character is any digits, Then the total number of ways will be the number of ways to decode uptill the previous digit only. That is .

Below is the implementation of the above approach:

## C++

`// C++ implementation to count the ` `// possible decodings of the given ` `// digit sequence ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count the number of ` `// ways to decode the given digit ` `// sequence ` `int` `waysToDecode2(string s) { ` ` ` `int` `n = s.size(); ` ` ` ` ` `// Array to store the dp states ` ` ` `vector<` `int` `> dp(n+1,0); ` ` ` ` ` `// Case of empty string ` ` ` `dp[0]=1; ` ` ` ` ` `// Condition to check if the ` ` ` `// first character of string is 0 ` ` ` `if` `(s[0]==` `'0'` `) ` ` ` `return` `0; ` ` ` ` ` `// Base case for single length ` ` ` `// string ` ` ` `dp[1]= ((s[0]==` `'*'` `)? 9 : 1); ` ` ` ` ` `// Bottom-up dp for the string ` ` ` `for` `(` `int` `i=2;i<=n;i++) ` ` ` `{ ` ` ` `// Previous character ` ` ` `char` `first= s[i-2]; ` ` ` ` ` `// Current character ` ` ` `char` `second= s[i-1]; ` ` ` ` ` `// Case to include the Current ` ` ` `// digit as a single digit for ` ` ` `// decoding the string ` ` ` `if` `(second==` `'*'` `) ` ` ` `{ ` ` ` `dp[i]+= 9*dp[i-1]; ` ` ` `} ` ` ` `else` `if` `(second>` `'0'` `) ` ` ` `dp[i]+=dp[i-1]; ` ` ` ` ` `// Case to include the current ` ` ` `// character as two-digit for ` ` ` `// decoding the string ` ` ` `if` `(first==` `'1'` `|| first==` `'2'` `) ` ` ` `{ ` ` ` ` ` `// Condition to check if the ` ` ` `// current character is "*" ` ` ` `if` `(second==` `'*'` `) ` ` ` `{ ` ` ` `if` `(first==` `'1'` `) ` ` ` `dp[i]+= 9 * dp[i-2]; ` ` ` `else` `if` `(first==` `'2'` `) ` ` ` `dp[i]+= 6 * dp[i-2]; ` ` ` `} ` ` ` ` ` `// Condition to check if the ` ` ` `// current character is less than ` ` ` `// or equal to 26 ` ` ` `else` `if` `(((first-` `'0'` `)* 10 + ` ` ` `(second-` `'0'` `))<= 26) ` ` ` `dp[i]+=dp[i-2]; ` ` ` `} ` ` ` ` ` `// Condition to check if the ` ` ` `// Previous digit is equal to "*" ` ` ` `else` `if` `(first==` `'*'` `) ` ` ` `{ ` ` ` `if` `(second==` `'*'` `) ` ` ` `{ ` ` ` `dp[i]+= 15 * dp[i-2]; ` ` ` `} ` ` ` `else` `if` `(second<=` `'6'` `) ` ` ` `dp[i]+= 2* dp[i-2]; ` ` ` `else` ` ` `dp [i]+= dp[i-2]; ` ` ` `} ` ` ` `} ` ` ` `return` `dp[n]; ` `} ` ` ` `// Driver Code ` `int` `main() { ` ` ` `string str = ` `"12*3"` `; ` ` ` ` ` `// Function Call ` ` ` `cout << waysToDecode2(str) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to count the ` `// possible decodings of the given ` `// digit sequence ` `class` `GFG{ ` ` ` `// Function to count the number of ` `// ways to decode the given digit ` `// sequence ` `static` `int` `waysToDecode2(` `char` `[]s) ` `{ ` ` ` ` ` `int` `n = s.length; ` ` ` ` ` `// Array to store the dp states ` ` ` `int` `[]dp = ` `new` `int` `[n + ` `1` `]; ` ` ` ` ` `// Case of empty String ` ` ` `dp[` `0` `] = ` `1` `; ` ` ` ` ` `// Condition to check if the ` ` ` `// first character of String is 0 ` ` ` `if` `(s[` `0` `] == ` `'0'` `) ` ` ` `return` `0` `; ` ` ` ` ` `// Base case for single length ` ` ` `// String ` ` ` `dp[` `1` `] = ((s[` `0` `] == ` `'*'` `) ? ` `9` `: ` `1` `); ` ` ` ` ` `// Bottom-up dp for the String ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i++) ` ` ` `{ ` ` ` ` ` `// Previous character ` ` ` `char` `first = s[i - ` `2` `]; ` ` ` ` ` `// Current character ` ` ` `char` `second = s[i - ` `1` `]; ` ` ` ` ` `// Case to include the Current ` ` ` `// digit as a single digit for ` ` ` `// decoding the String ` ` ` `if` `(second == ` `'*'` `) ` ` ` `{ ` ` ` `dp[i] += ` `9` `* dp[i - ` `1` `]; ` ` ` `} ` ` ` `else` `if` `(second > ` `'0'` `) ` ` ` `dp[i] += dp[i - ` `1` `]; ` ` ` ` ` `// Case to include the current ` ` ` `// character as two-digit for ` ` ` `// decoding the String ` ` ` `if` `(first == ` `'1'` `|| first == ` `'2'` `) ` ` ` `{ ` ` ` ` ` `// Condition to check if the ` ` ` `// current character is "*" ` ` ` `if` `(second == ` `'*'` `) ` ` ` `{ ` ` ` `if` `(first == ` `'1'` `) ` ` ` `dp[i] += ` `9` `* dp[i - ` `2` `]; ` ` ` `else` `if` `(first == ` `'2'` `) ` ` ` `dp[i] += ` `6` `* dp[i - ` `2` `]; ` ` ` `} ` ` ` ` ` `// Condition to check if the ` ` ` `// current character is less than ` ` ` `// or equal to 26 ` ` ` `else` `if` `(((first - ` `'0'` `) * ` `10` `+ ` ` ` `(second - ` `'0'` `)) <= ` `26` `) ` ` ` `{ ` ` ` `dp[i] += dp[i - ` `2` `]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Condition to check if the ` ` ` `// previous digit is equal to "*" ` ` ` `else` `if` `(first == ` `'*'` `) ` ` ` `{ ` ` ` `if` `(second == ` `'*'` `) ` ` ` `{ ` ` ` `dp[i] += ` `15` `* dp[i - ` `2` `]; ` ` ` `} ` ` ` `else` `if` `(second <= ` `'6'` `) ` ` ` `{ ` ` ` `dp[i] += ` `2` `* dp[i - ` `2` `]; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `dp[i] += dp[i - ` `2` `]; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `dp[n]; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `String str = ` `"12*3"` `; ` ` ` ` ` `// Function Call ` ` ` `System.out.print(waysToDecode2( ` ` ` `str.toCharArray()) + ` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to count the ` `// possible decodings of the given ` `// digit sequence ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to count the number of ` `// ways to decode the given digit ` `// sequence ` `static` `int` `waysToDecode2(` `char` `[]s) ` `{ ` ` ` `int` `n = s.Length; ` ` ` ` ` `// Array to store the dp states ` ` ` `int` `[]dp = ` `new` `int` `[n + 1]; ` ` ` ` ` `// Case of empty String ` ` ` `dp[0] = 1; ` ` ` ` ` `// Condition to check if the ` ` ` `// first character of String is 0 ` ` ` `if` `(s[0] == ` `'0'` `) ` ` ` `return` `0; ` ` ` ` ` `// Base case for single length ` ` ` `// String ` ` ` `dp[1] = ((s[0] == ` `'*'` `) ? 9 : 1); ` ` ` ` ` `// Bottom-up dp for the String ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `{ ` ` ` ` ` `// Previous character ` ` ` `char` `first = s[i - 2]; ` ` ` ` ` `// Current character ` ` ` `char` `second = s[i - 1]; ` ` ` ` ` `// Case to include the current ` ` ` `// digit as a single digit for ` ` ` `// decoding the String ` ` ` `if` `(second == ` `'*'` `) ` ` ` `{ ` ` ` `dp[i] += 9 * dp[i - 1]; ` ` ` `} ` ` ` `else` `if` `(second > ` `'0'` `) ` ` ` `{ ` ` ` `dp[i] += dp[i - 1]; ` ` ` `} ` ` ` ` ` `// Case to include the current ` ` ` `// character as two-digit for ` ` ` `// decoding the String ` ` ` `if` `(first == ` `'1'` `|| first == ` `'2'` `) ` ` ` `{ ` ` ` ` ` `// Condition to check if the ` ` ` `// current character is "*" ` ` ` `if` `(second == ` `'*'` `) ` ` ` `{ ` ` ` `if` `(first == ` `'1'` `) ` ` ` `{ ` ` ` `dp[i] += 9 * dp[i - 2]; ` ` ` `} ` ` ` `else` `if` `(first == ` `'2'` `) ` ` ` `{ ` ` ` `dp[i] += 6 * dp[i - 2]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Condition to check if the ` ` ` `// current character is less than ` ` ` `// or equal to 26 ` ` ` `else` `if` `(((first - ` `'0'` `) * 10 + ` ` ` `(second - ` `'0'` `)) <= 26) ` ` ` `{ ` ` ` `dp[i] += dp[i - 2]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Condition to check if the ` ` ` `// previous digit is equal to "*" ` ` ` `else` `if` `(first == ` `'*'` `) ` ` ` `{ ` ` ` `if` `(second == ` `'*'` `) ` ` ` `{ ` ` ` `dp[i] += 15 * dp[i - 2]; ` ` ` `} ` ` ` `else` `if` `(second <= ` `'6'` `) ` ` ` `{ ` ` ` `dp[i] += 2 * dp[i - 2]; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `dp[i] += dp[i - 2]; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `dp[n]; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `String str = ` `"12*3"` `; ` ` ` ` ` `// Function Call ` ` ` `Console.Write(waysToDecode2( ` ` ` `str.ToCharArray()) + ` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

**Output:**

28

**Performance Analysis:**

**Time Complexity:**O(N)**Auxiliary Space:**O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count Possible Decodings of a given Digit Sequence
- Count Possible Decodings of a given Digit Sequence in O(N) time and Constant Auxiliary space
- Count of N-digit numbers having digit XOR as single digit
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count numbers from 1 to n that have 4 as a digit
- Count numbers less than N containing digits from the given set : Digit DP
- Count of integers from the range [0, N] whose digit sum is a multiple of K
- Count numbers (smaller than or equal to N) with given digit sum
- Count digit groupings of a number with given constraints
- Count numbers with exactly K non-zero digits and distinct odd digit sum
- Count of n digit numbers whose sum of digits equals to given sum
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M
- Count of N-digit numbers in base K with no two consecutive zeroes
- Count of Numbers in a Range where digit d occurs exactly K times
- Count of numbers in range which are divisible by M and have digit D at odd places
- Queries for count of even digit sum elements in given range using MO's Algorithm
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Count of N digit Numbers having no pair of equal consecutive Digits

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.