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 == "*":
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
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 <
View Solution →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
View Solution →Binary Search Tree : Lowest Common Ancestor
You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b
View Solution →Swap Nodes [Algo]
A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from
View Solution →Kitty's Calculations on a Tree
Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a
View Solution →Is This a Binary Search Tree?
For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a
View Solution →