Count all possible texts that can be formed from Number using given mapping

Count all possible texts that can be formed from Number using given mapping

Given a number N and a mapping of letters to each integer from 1 to 8, which are: 
{1: ‘abc’, 2: ‘def’, 3: ‘ghi’, 4: ‘jkl’, 5: ‘mno’, 6: ‘pqr’, 7: ‘stu’, 8: ‘wxyz’}
The mapping denotes that a single 1 can be replaced by ‘a’, a pair of 1 can be replaced by ‘b’ and a triplet of 1 can be replaced by ‘c’. Similarly for all other digits. The task is to find the total possible number of texts formed by replacing the digits of the given N.

Examples: 

Input: N = 22233
Output: 8
Explanation:  All the possible texts are dddgg, dddh, edgg, edh, degg, deh, fgg, fh.
So the total number texts that can be interpreted is 8.

Input: N = 88881
Output: 8

Naive Approach: The approach is to generate all possible combinations using recursion and count the total possible texts.

Time Complexity: O(2D) where D is the total number of digits in the N
Auxiliary Space: O(1)

Efficient Approach: This problem can be solved using dynamic programming using the following idea:

At a time the number of occurrences which can be considered for each digit is 1, 2 or 3 (4 occurrences only for digit 8). 

Consider the number of ways to form a string using the digits after ith position from left is denoted as f(i).
Therefore, from the above observation it can be said that:
f(i) = f(i+1) + f(i+2) + f(i+3) [+f(i+4) if the digit is 8]. because continuous one, two, or three occurrences of a number can be expressed using a single letter.

It can be seen that If Ni ≠ Ni+1 then f(i) = f(i+1)
If Ni = Ni+1 then the equation holds only till the 2nd term i.e f(i) = f(i+1) + f(i+2).
Similarly for other cases like Ni = Ni + 1 = Ni + 2 and all.

So it can be said that f(i) = j = 1 to m∑ f(i + j) where m is the number of continuous occurrences of Ni and m does not exceed 3 (4 when the digit is ‘8’).

Follow the steps mentioned below to implement the idea:

  • Declare a dp[] array of size string(s) length and initialize it to -1 to store the values calculated till now.
  • Use a recursive function to implement the above functional relations and call from 0th index.
    • If the current index is out of string then return 1
    • If the value for the current index is already calculated (i.e, dp[] value is not -1) then return the value stored in dp[].
    • Now check the index till which there is a continuous occurrence of the current digit and calculate the value of the above function accordingly.
    • Call the recursive function for the next index and continue the process.
    • Store the answer for the current index (say i) in dp[i] and return the same to the previous call.
  • The value returned for the first index is the required total number of ways.

Below is the implementation of the above approach:

C++

  

#include <bits/stdc++.h>

using namespace std;

  

long countPossibilities(string s, int idx,

                        vector<long>& dp)

{

    

    

    

    if (idx > s.length())

        return 1;

  

    

    

    if (dp[idx] != -1)

        return dp[idx];

  

    

    int ans = 0;

  

    

    

    

    if (idx + 1 < s.length() && s[idx] == s[idx + 1]) {

        ans = ans + countPossibilities(s, idx + 2,

                                       dp);

  

        

        

        if (idx + 2 < s.length() && s[idx] == s[idx + 2]) {

            ans = ans + countPossibilities(s,

                                           idx + 3, dp);

  

            

            

            

            

            if (idx + 3 < s.length() && (s[idx] == '8')

                && s[idx] == s[idx + 3]) {

                ans += countPossibilities(s, idx + 4,

                                          dp);

            }

        }

    }

  

    

    

    ans += countPossibilities(s, idx + 1, dp);

  

    

    dp[idx] = ans;

  

    

    return dp[idx];

}

  

int main()

{

    string s = "88881";

    vector<long> dp(s.length() + 1, -1);

  

    

    cout << countPossibilities(s, 0, dp);

    return 0;

}

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