A message containing letters from A-Z can be encoded into numbers using the following mapping:
1
2
3
4
'A'->"1"'B'->"2"...'Z'->"26"
To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, "11106" can be mapped into:
"AAJF" with the grouping (1 1 10 6)
"KJF" with the grouping (11 10 6)
Note that the grouping (1 11 06) is invalid because "06" cannot be mapped into 'F' since "6" is different from "06".
In addition to the mapping above, an encoded message may contain the '*' character, which can represent any digit from '1' to '9' ('0' is excluded). For example, the encoded message "1*" may represent any of the encoded messages "11", "12", "13", "14", "15", "16", "17", "18", or "19". Decoding "1*" is equivalent to decoding any of the encoded messages it can represent.
Given a string s consisting of digits and '*' characters, return the number of ways to decode it.
Since the answer may be very large, return it modulo10^9 + 7.
Input: s ="*"Output: 9Explanation: The encoded message can represent any of the encoded messages "1","2","3","4","5","6","7","8", or "9".Each of these can be decoded to the strings "A","B","C","D","E","F","G","H", and "I" respectively.Hence, there are a total of 9 ways to decode "*".
Example 2:
1
2
3
4
5
Input: s ="1*"Output: 18Explanation: The encoded message can represent any of the encoded messages "11","12","13","14","15","16","17","18", or "19".Each of these encoded messages have 2 ways to be decoded(e.g."11" can be decoded to "AA" or "K").Hence, there are a total of 9*2=18 ways to decode "1*".
Example 3:
1
2
3
4
5
Input: s ="2*"Output: 15Explanation: The encoded message can represent any of the encoded messages "21","22","23","24","25","26","27","28", or "29"."21","22","23","24","25", and "26" have 2 ways of being decoded, but "27","28", and "29" only have 1 way.Hence, there are a total of(6*2)+(3*1)=12+3=15 ways to decode "2*".
The problem is an extension of the classic “Decode Ways” problem, but with the wildcard * that can represent digits 1-9. We use dynamic programming to count the number of ways to decode the string, considering both single and double character decodings at each step. The key is to handle the * character carefully for both single and double digit possibilities.
classSolution {
public:int numDecodings(string s) {
int mod =1e9+7, n = s.size();
long a =1, b =1, ans =0;
for (int i =0; i < n; ++i) {
ans =0;
if (s[i] =='*') ans = (b *9) % mod;
elseif (s[i] !='0') ans = b;
if (i >0) {
if (s[i-1] =='*'&& s[i] =='*') ans = (ans + a *15) % mod;
elseif (s[i-1] =='*') {
if (s[i] <='6') ans = (ans + a *2) % mod;
else ans = (ans + a) % mod;
} elseif (s[i] =='*') {
if (s[i-1] =='1') ans = (ans + a *9) % mod;
elseif (s[i-1] =='2') ans = (ans + a *6) % mod;
} else {
int num = (s[i-1] -'0') *10+ (s[i] -'0');
if (num >=10&& num <=26) ans = (ans + a) % mod;
}
}
a = b;
b = ans;
}
return ans;
}
}
classSolution {
publicintnumDecodings(String s) {
int mod = 1000000007, n = s.length();
long a = 1, b = 1, ans = 0;
for (int i = 0; i < n; ++i) {
ans = 0;
if (s.charAt(i) =='*') ans = (b * 9) % mod;
elseif (s.charAt(i) !='0') ans = b;
if (i > 0) {
char p = s.charAt(i-1), c = s.charAt(i);
if (p =='*'&& c =='*') ans = (ans + a * 15) % mod;
elseif (p =='*') {
if (c <='6') ans = (ans + a * 2) % mod;
else ans = (ans + a) % mod;
} elseif (c =='*') {
if (p =='1') ans = (ans + a * 9) % mod;
elseif (p =='2') ans = (ans + a * 6) % mod;
} else {
int num = (p -'0') * 10 + (c -'0');
if (num >= 10 && num <= 26) ans = (ans + a) % mod;
}
}
a = b;
b = ans;
}
return (int)ans;
}
}
classSolution {
funnumDecodings(s: String): Int {
val mod = 1_000_000_007
var a = 1Lvar b = 1Lvar ans = 0Lfor (i in s.indices) {
ans = 0Lif (s[i] =='*') ans = (b * 9) % mod
elseif (s[i] !='0') ans = b
if (i > 0) {
val p = s[i-1]
val c = s[i]
if (p =='*'&& c =='*') ans = (ans + a * 15) % mod
elseif (p =='*') {
if (c <='6') ans = (ans + a * 2) % mod
else ans = (ans + a) % mod
} elseif (c =='*') {
if (p =='1') ans = (ans + a * 9) % mod
elseif (p =='2') ans = (ans + a * 6) % mod
} else {
val num = (p - '0') * 10 + (c - '0')
if (num in10..26) ans = (ans + a) % mod
}
}
a = b
b = ans
}
return ans.toInt()
}
}
classSolution:
defnumDecodings(self, s: str) -> int:
mod: int =10**9+7 a: int =1 b: int =1 ans: int =0for i, ch in enumerate(s):
ans =0if ch =='*':
ans = (b *9) % mod
elif ch !='0':
ans = b
if i >0:
p = s[i-1]
if p =='*'and ch =='*':
ans = (ans + a *15) % mod
elif p =='*':
if ch <='6':
ans = (ans + a *2) % mod
else:
ans = (ans + a) % mod
elif ch =='*':
if p =='1':
ans = (ans + a *9) % mod
elif p =='2':
ans = (ans + a *6) % mod
else:
num = int(p) *10+ int(ch)
if10<= num <=26:
ans = (ans + a) % mod
a, b = b, ans
return ans
impl Solution {
pubfnnum_decodings(s: String) -> i32 {
let m =1_000_000_007;
let (mut a, mut b, mut ans) = (1i64, 1i64, 0i64);
let s = s.as_bytes();
for i in0..s.len() {
ans =0;
if s[i] ==b'*' {
ans = (b *9) % m;
} elseif s[i] !=b'0' {
ans = b;
}
if i >0 {
let p = s[i-1];
let c = s[i];
if p ==b'*'&& c ==b'*' {
ans = (ans + a *15) % m;
} elseif p ==b'*' {
if c <=b'6' {
ans = (ans + a *2) % m;
} else {
ans = (ans + a) % m;
}
} elseif c ==b'*' {
if p ==b'1' {
ans = (ans + a *9) % m;
} elseif p ==b'2' {
ans = (ans + a *6) % m;
}
} else {
let num = (p -b'0') asi32*10+ (c -b'0') asi32;
if num >=10&& num <=26 {
ans = (ans + a) % m;
}
}
}
a = b;
b = ans;
}
ans asi32 }
}