# 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 :

```                        ```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 == "*":
count2 += 9
else:
count2 += 1
if c == "2" or c == "*":
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)

return count```
```

## Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your \$inOrder* func

## Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

## Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

## Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

## Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

## Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t