Decode Messages Sequel - Google Top Interview Questions


Problem Statement :


Given the mapping a = 1, b = 2, ... z = 26, as well as "*" which can be mapped anything from 1 to 9, and an encoded message message (as a string), count the number of ways it can be decoded.

Mod the result by 10 ** 9 + 7.

Constraints

n ≤ 100,000 where n is the length of message

Example 1

Input

message = "1*"

Output

18

Explanation

The "*" can represent anything from 1 to 9, so this can be decoded as:



["aa", "ab", "ac", "ad", "ae", "af", "ag", "ah", "ai"] - (1, 1), (1, 2), ..., (1, 9)`

["k", "l", "m", "n", "o", "p", "q", "r", "s"] (11), (12), ..., (19)

Example 2

Input

message = "22"

Output

2

Explanation

This can represent "bb" or "v"



Example 3

Input

message = "*00"

Output

0

Explanation

There's no valid decoding



Solution :



title-img




                        Solution in C++ :

#define int unsigned long long
int mod = 1000000007;
int helper(string& s, vector<int>& dp, int i) {
    if (i == s.length()) return 1;
    if (dp[i] != -1) return dp[i] % mod;
    if (s[i] == '0') return 0;
    int ans = 0;
    if (s[i] == '*') {
        for (int j = 1; j <= 9; j++) {
            ans += helper(s, dp, i + 1) % mod;
            ans %= mod;
            if (i + 1 < s.length() and s[i + 1] == '*') {
                for (int k = 1; k <= 9 and j <= 2; k++) {
                    if (j * 10 + k <= 26) {
                        ans += helper(s, dp, i + 2) % mod;
                        ans %= mod;
                    }
                }
            } else if (i + 1 < s.length()) {
                if (j * 10 + s[i + 1] - '0' <= 26) {
                    ans += helper(s, dp, i + 2) % mod;
                    ans %= mod;
                }
            }
        }
    } else {
        ans += helper(s, dp, i + 1) % mod;
        ans %= mod;
        if (i + 1 < s.length() and s[i + 1] == '*') {
            for (int k = 1; k <= 9; k++) {
                if ((s[i] - '0') * 10 + k <= 26) {
                    ans += helper(s, dp, i + 2) % mod;
                    ans %= mod;
                }
            }
        } else if (i + 1 < s.length()) {
            if ((s[i] - '0') * 10 + s[i + 1] - '0' <= 26) {
                ans += helper(s, dp, i + 2) % mod;
                ans %= mod;
            }
        }
    }
    return dp[i] = ans;
}
int32_t solve(string mess) {
    int n = mess.length();
    vector<int> dp(mess.length() + 1, -1);
    return helper(mess, dp, 0);
}
                    


                        Solution in Java :

import java.util.*;

class Solution {
    static final long MOD = 1000000007L;
    public int solve(String s) {
        int N = s.length();
        long[] dp = new long[N + 1];
        dp[0] = 1L;
        for (int i = 1; i <= N; i++) {
            dp[i] = dp[i - 1] * one(s.charAt(i - 1));

            if (i > 1) {
                dp[i] += dp[i - 2] * two(s.charAt(i - 2), s.charAt(i - 1));
            }
            dp[i] %= MOD;
        }
        return (int) (dp[N]);
    }

    public int one(char c) {
        if (c == '*')
            return 9;
        else if (c == '0')
            return 0;
        else
            return 1;
    }

    public int two(char a, char b) {
        if (a == '0')
            return 0;

        int AL = 1;
        int AR = 2;
        if (a != '*') {
            AL = a - '0';
            AR = a - '0';
        }
        int BL = 1;
        int BR = 9;
        if (b != '*') {
            BL = b - '0';
            BR = b - '0';
        }

        int ans = 0;
        for (int i = AL; i <= AR; i++) {
            for (int j = BL; j <= BR; j++) {
                if (10 * i + j <= 26)
                    ans++;
            }
        }
        return ans;
    }
}
                    


                        Solution in Python : 
                            
class Solution:
    def solve(self, message):

        n = len(message)
        count = 1  # current count
        pcount = 1  # previous count

        follow = None  # char from previous iteration
        for i in range(1, n + 1):
            c = message[-i]

            # count 2 character groups
            count2 = 0
            if i != 1:
                if c == "1" or c == "*":
                    if follow == "*":
                        count2 += 9
                    else:
                        count2 += 1
                if c == "2" or c == "*":
                    if follow == "*":
                        count2 += 6
                    elif ord("0") <= ord(follow) <= ord("6"):
                        count2 += 1

            # count 1 character groups
            if c == "*":
                count1 = 9
            elif c == "0":
                count1 = 0
            else:
                count1 = 1

            # sum and mod
            ncount = count * count1 + pcount * count2
            pcount = count
            count = ncount % (10 ** 9 + 7)
            follow = c

        return count
                    


View More Similar Problems

Lazy White Falcon

White Falcon just solved the data structure problem below using heavy-light decomposition. Can you help her find a new solution that doesn't require implementing any fancy techniques? There are 2 types of query operations that can be performed on a tree: 1 u x: Assign x as the value of node u. 2 u v: Print the sum of the node values in the unique path from node u to node v. Given a tree wi

View Solution →

Ticket to Ride

Simon received the board game Ticket to Ride as a birthday present. After playing it with his friends, he decides to come up with a strategy for the game. There are n cities on the map and n - 1 road plans. Each road plan consists of the following: Two cities which can be directly connected by a road. The length of the proposed road. The entire road plan is designed in such a way that if o

View Solution →

Heavy Light White Falcon

Our lazy white falcon finally decided to learn heavy-light decomposition. Her teacher gave an assignment for her to practice this new technique. Please help her by solving this problem. You are given a tree with N nodes and each node's value is initially 0. The problem asks you to operate the following two types of queries: "1 u x" assign x to the value of the node . "2 u v" print the maxim

View Solution →

Number Game on a Tree

Andy and Lily love playing games with numbers and trees. Today they have a tree consisting of n nodes and n -1 edges. Each edge i has an integer weight, wi. Before the game starts, Andy chooses an unordered pair of distinct nodes, ( u , v ), and uses all the edge weights present on the unique path from node u to node v to construct a list of numbers. For example, in the diagram below, Andy

View Solution →

Heavy Light 2 White Falcon

White Falcon was amazed by what she can do with heavy-light decomposition on trees. As a resut, she wants to improve her expertise on heavy-light decomposition. Her teacher gave her an another assignment which requires path updates. As always, White Falcon needs your help with the assignment. You are given a tree with N nodes and each node's value Vi is initially 0. Let's denote the path fr

View Solution →

Library Query

A giant library has just been inaugurated this week. It can be modeled as a sequence of N consecutive shelves with each shelf having some number of books. Now, being the geek that you are, you thought of the following two queries which can be performed on these shelves. Change the number of books in one of the shelves. Obtain the number of books on the shelf having the kth rank within the ra

View Solution →