# Count Possible Decodings of a given Digit Sequence in O(N) time and Constant Auxiliary space

Given a digit sequence **S**, the task is to find the number of possible decodings of the given digit sequence where 1 represents ‘A’, 2 represents ‘B’ … and so on up to 26, where 26 represents ‘Z’.

**Examples:**

Input:S = “121”

Output:3

The possible decodings are “ABA”, “AU”, “LA”

Input:S = “1234”

Output:3

The possible decodings are “ABCD”, “LCD”, “AWD”

**Approach:** In order to solve this problem in O(N) time complexity, Dynamic Programming is used. And in order to reduce the auxiliary space complexity to O(1), we use the space optimized version of recurrence relation discussed in the Fibonacci Number Post.

Similar to the Fibonacci Numbers, the key observations of any current ‘i^{th}‘ index can be calculated using its previous two indices. So the Recurrence Relation to calculate the i^{th} index can be denoted as

// Condition to check last // digit can be included or not if (digit[i-1] is not '0') count[i] += count[i-1] // Condition to check the last // two digits contribution if (digit[i-2] is 1 or (digit[i-2] is 2 and digit[i-1] is less than 7)) count[i] += count[i-2]

Below is the implementation of the above approach:

## C++

`// C++ implementation to count decodings ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// A Dynamic Programming based function ` `// to count decodings in digit sequence ` `int` `countDecodingDP(string digits, ` `int` `n) ` `{ ` ` ` `// For base condition "01123" ` ` ` `// should return 0 ` ` ` `if` `(digits[0] == ` `'0'` `) ` ` ` `return` `0; ` ` ` ` ` `int` `count0 = 1, count1 = 1, count2; ` ` ` ` ` `// Using last two calculated values, ` ` ` `// calculate for ith index ` ` ` `for` `(` `int` `i = 2; i <= n; i++) { ` ` ` `count2 = ((` `int` `)(digits[i - 1] != ` `'0'` `) * count1) + ` ` ` `(` `int` `)((digits[i - 2] == ` `'1'` `) or ` ` ` `(digits[i - 2] == ` `'2'` `and ` ` ` `digits[i - 1] < ` `'7'` `)) * count0; ` ` ` `count0 = count1; ` ` ` `count1 = count2; ` ` ` `} ` ` ` ` ` `// Return the required answer ` ` ` `return` `count1; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `string digits = ` `"1234"` `; ` ` ` `int` `n = digits.size(); ` ` ` ` ` `// Function call ` ` ` `cout << countDecodingDP(digits, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(N)

**Auxiliary Space Complexity:** O(1)

**Related Article:** Count Possible Decodings of a given Digit Sequence

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count Possible Decodings of a given Digit Sequence
- Count possible decodings of a given Digit Sequence | Set 2
- Dynamic Programming | Wildcard Pattern Matching | Linear Time and Constant Space
- Sum of all substrings of a string representing a number | Set 2 (Constant Extra Space)
- 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
- Number of n digit stepping numbers | Space optimized solution
- Space and time efficient Binomial Coefficient
- 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 digit groupings of a number with given constraints
- Count numbers (smaller than or equal to N) with given digit sum
- Count of integers from the range [0, N] whose digit sum is a multiple of K
- 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 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
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1

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.