# Decode Ways II

• When string is "0", it returns 1, should be 0. To fix it, second = s.charAt(0) == '*' ? 9 : s.charAt(0) == '0' ? 0 : 1;

• @travellinglina Thanks for pointing it out. I have fixed the code.

• Example 2:
Input: "1*"
Output: 9 + 9 = 18

This is wrong. "1*"'s '*' can vary from 0 to 9 to make "10"(J) to "19"(S) which is 10.
J(10), K(11), L(12), M(13), N(14), O(15), P(16), Q(17), R(18), S(19), this is 10.

So 9 + 10 = 19. Not 18, Please correct it. Thank you.

• @Aeonaxx * rages from 1 to 9. It cant be 0.

• Hi, In the last lined in the below snippet, I believe it should be 17 instead of 15. Since, given that first char is * and second char is *, total no of 2 digit characters are 10,11...26 = 17 (K, L, M, N, O, ....Z)

Please correct if wrong

``````if (s.charAt(i) == '*') {
second = 9 * second;
if (s.charAt(i - 1) == '1')
second = (second + 9 * first) % M;
else if (s.charAt(i - 1) == '2')
second = (second + 6 * first) % M;
else if (s.charAt(i - 1) == '*')
second = (second + 15 * first) % M;
}
``````

• class Solution {
public:
int maxMod;
int numDecodings(string s) {
maxMod = 1000000007;
//vector<int> dp(s.size(), -1);
if (s.size() == 0)
return 0;
if (s.size() == 1)
return getSValue(s[0]);
if (s.size() == 2)
return getValue(s[0], s[1]) + getSValue(s[0])getSValue(s[1]);
long lastOne = getSValue(s[s.size()-1]);
long lastTwo= getValue(s[s.size()-2], s[s.size()-1]) + getSValue(s[s.size()-2])getSValue(s[s.size()-1]);
for (int i = s.size() - 3; i >= 0; i--)
{
long cur = ((getSValue(s[i])lastTwo) % maxMod + (getValue(s[i], s[i + 1])lastOne) % maxMod) % maxMod;
lastOne = lastTwo;
lastTwo = cur;
}
return lastTwo;
}
long getValue(char x, char y)
{
if (x == '0')
return 0;
if (x != '
'&&y != '
')
{
if ((x - '0') * 10 + y - '0' <= 26)
return 1;
else
return 0;
}
else
{
if (x != '
')
{
switch (x)
{
case '1':return 9;
case '2':return 6;
default:return 0;
break;
}
}
else
{
if (y != '
')
{
if (y - '0' < 7)
return 2;
else
return 1;
}
else
return 15;
}
}
}
long getSValue(char x)
{
if (x != ''&&x != '0')
return 1;
if (x == '
')
return 9;
return 0;
}
};

• there is an input case, '101010‘, it can be decoded as 'JJJ',so the answer is 1, but the " Custom Testcase " gives 0。

• and '20' has the same problem

• @vinod23

The tests are broken.
"2*" can be decoded 16 ways, not 15.

2 1 ba
2 2 bb
2 3 bc
2 4 bd
2 5 be
2 6 bf
2 7 bg
2 8 bh
2 9 bi

20 t
21 u
22 v
23 w
24 x
25 y
26 z

Why are you so mean to letter "T"?
Are you saying that "2*" cannot mean "20" = "T" ? That's just absurd.

• @Ark-kun
Pls double check the description: the character '*', which can be treated as one of the numbers from 1 to 9.

• can't be '0'.

• Why does it need to mod 1000000007

• I use int instead of long to store the result. Because multiplication will cause overflow, I add the number 9 times instead of multiply it by 9. Does anyone know some more efficient method?

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.