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

Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

View Solution →

Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

View Solution →

Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →

AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

View Solution →

Waiter

You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

View Solution →