Decode Ways II in C++

C++Server Side ProgrammingProgramming

Suppose there is a message, that is containing letters from A-Z is being encoded to numbers using the following mapping way −

'A' -> 1, 'B' -> 2, ... , 'Z' -> 26

Now, the encoded string can also contain the character '*', which can be treated as one of the numbers from 1 to 9. So if we have the encoded message containing digits and the character '*', then we have to find the total number of ways to decode it. If the answer is very long, we can use mod 109 + 7 to get the final result. So if the input is only *, then there may be 9 possible ways, these are all numbers from 1 to 9, so these are A to I.

To solve this, we will follow these steps −

  • Define a function add(), this will take a, b,
  • return ((a mod m) + (b mod m)) mod m
  • Define a function mul(), this will take a, b,
  • return ((a mod m) * (b mod m)) mod m
  • From the main method do the following −
  • n := size of s
  • Define an array dp of size n + 1
  • dp[0] := 1
  • if s[0] is same as '0', then −
    • return 0
  • dp[1] := 9 when s[0] is same as ' * ' otherwise 1
  • for initialize i := 2, when i <= n, update (increase i by 1), do −
    • first := s[i - 2], second := s[i - 1]
    • if second is same as '*', then −
      • dp[i] := add(dp[i], mul(9, dp[i - 1]))
    • otherwise when second > '0', then −
      • dp[i] := dp[i - 1]
    • if first is same as '*', then −
      • if second is same as '*', then −
        • dp[i] := add(dp[i], mul(15, dp[i - 2]))
      • otherwise when second <= '6', then −
        • dp[i] := add(dp[i], mul(2, dp[i - 2]))
      • Otherwise
        • dp[i] := add(dp[i], mul(1, dp[i - 2]))
    • otherwise when first is same as '1' or first is same as '2', then −
      • if second is same as '*', then −
        • if first is same as '1', then −
          • dp[i] := add(dp[i], mul(9, dp[i - 2]))
        • otherwise when first is same as '2', then −
          • dp[i] := add(dp[i], mul(6, dp[i - 2]))
      • otherwise when (first - '0') * 10 + (second - '0') <= 26, then −
        • dp[i] := add(dp[i], dp[i - 2])
  • return dp[n]

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
const lli m = 1e9 + 7;
class Solution {
public:
   lli add(lli a, lli b){
      return ((a % m) + (b % m)) % m;
   }
   lli mul(lli a, lli b){
      return ((a % m) * (b % m)) % m;
   }
   int numDecodings(string s) {
      int n = s.size();
      vector <int> dp(n + 1);
      dp[0] = 1;
      if(s[0] == '0') return 0;
      dp[1] = s[0] == '*' ? 9 : 1;
      for(int i = 2; i <= n; i++){
         char first = s[i - 2];
         char second = s[i - 1];
         if(second == '*'){
            dp[i] = add(dp[i], mul(9, dp[i - 1]));
         }else if(second > '0'){
            dp[i] = dp[i - 1];
         }
         if(first == '*'){
            if(second == '*'){
               dp[i] = add(dp[i], mul(15, dp[i - 2]));
            }else if (second <= '6'){
               dp[i] = add(dp[i], mul(2, dp[i - 2]));
            }else{
               dp[i] = add(dp[i], mul(1, dp[i - 2]));
            }
         }else if(first == '1' || first == '2'){
            if(second == '*'){
               if(first == '1'){
                  dp[i] = add(dp[i], mul(9, dp[i - 2]));
               }else if(first == '2'){
                  dp[i] = add(dp[i], mul(6, dp[i - 2]));
               }
            }else if((first - '0') * 10 + (second - '0') <= 26){
               dp[i] = add(dp[i], dp[i - 2]);
            }
         }
      }
      return dp[n];
   }
};
main(){
   Solution ob;
   cout << (ob.numDecodings("2*"));
}

Input

“2*”

Output

15
raja
Published on 01-Jun-2020 11:51:36
Advertisements