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’.
Input: S = “121”
The possible decodings are “ABA”, “AU”, “LA”
Input: S = “1234”
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 ‘ith‘ index can be calculated using its previous two indices. So the Recurrence Relation to calculate the ith 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:
Time Complexity: O(N)
Auxiliary Space Complexity: O(1)
Related Article: Count Possible Decodings of a given Digit Sequence
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.
- 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
- Count of N-digit numbers having digit XOR as single digit
- Sum of all substrings of a string representing a number | Set 2 (Constant Extra Space)
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count of Numbers in Range where first digit is equal to last digit of the number
- Number of n digit stepping numbers | Space optimized solution
- Space and time efficient Binomial Coefficient
- Maximum K-digit number possible from subsequences of two given arrays
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Minimum sum possible of any bracket sequence of length N
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count possible permutations of given array satisfying the given conditions
- Count of n digit numbers whose sum of digits equals to given sum
- Count digit groupings of a number with given constraints
- Count numbers (smaller than or equal to N) with given digit sum
- Count numbers less than N containing digits from the given set : Digit DP
- 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 firstname.lastname@example.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.