Skip to content

Commit 911de8e

Browse files
committed
+ problem 639
1 parent fa76bb9 commit 911de8e

File tree

5 files changed

+280
-0
lines changed

5 files changed

+280
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# 639. Decode Ways II
2+
A message containing letters from `A-Z` can be **encoded** into numbers using the following mapping:
3+
4+
```
5+
'A' -> "1"
6+
'B' -> "2"
7+
...
8+
'Z' -> "26"
9+
```
10+
11+
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:
12+
13+
* `"AAJF"` with the grouping `(1 1 10 6)`
14+
* `"KJF"` with the grouping `(11 10 6)`
15+
16+
Note that the grouping `(1 11 06)` is invalid because `"06"` cannot be mapped into `'F'` since `"6"` is different from `"06"`.
17+
18+
**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.
19+
20+
Given a string `s` consisting of digits and `'*'` characters, return *the **number** of ways to **decode** it*.
21+
22+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
23+
24+
#### Example 1:
25+
<pre>
26+
<strong>Input:</strong> s = "*"
27+
<strong>Output:</strong> 9
28+
<strong>Explanation:</strong> The encoded message can represent any of the encoded messages "1", "2", "3", "4", "5", "6", "7", "8", or "9".
29+
Each of these can be decoded to the strings "A", "B", "C", "D", "E", "F", "G", "H", and "I" respectively.
30+
Hence, there are a total of 9 ways to decode "*".
31+
</pre>
32+
33+
#### Example 2:
34+
<pre>
35+
<strong>Input:</strong> s = "1*"
36+
<strong>Output:</strong> 18
37+
<strong>Explanation:</strong> The encoded message can represent any of the encoded messages "11", "12", "13", "14", "15", "16", "17", "18", or "19".
38+
Each of these encoded messages have 2 ways to be decoded (e.g. "11" can be decoded to "AA" or "K").
39+
Hence, there are a total of 9 * 2 = 18 ways to decode "1*".
40+
</pre>
41+
42+
#### Example 3:
43+
<pre>
44+
<strong>Input:</strong> s = "2*"
45+
<strong>Output:</strong> 15
46+
<strong>Explanation:</strong> The encoded message can represent any of the encoded messages "21", "22", "23", "24", "25", "26", "27", "28", or "29".
47+
"21", "22", "23", "24", "25", and "26" have 2 ways of being decoded, but "27", "28", and "29" only have 1 way.
48+
Hence, there are a total of (6 * 2) + (3 * 1) = 12 + 3 = 15 ways to decode "2*".
49+
</pre>
50+
51+
#### Constraints:
52+
* <code>1 <= s.length <= 10<sup>5</sup></code>
53+
* `s[i]` is a digit or `'*'`.
54+
55+
## Solutions (Rust)
56+
57+
### 1. Solution
58+
```Rust
59+
impl Solution {
60+
pub fn num_decodings(s: String) -> i32 {
61+
let s = s.as_bytes();
62+
let mut dp = vec![[0_i64; 2]; s.len()];
63+
64+
match s[0] {
65+
b'0' => return 0,
66+
b'*' => dp[0][0] = 9,
67+
_ => dp[0][0] = 1,
68+
}
69+
70+
if s.len() > 1 {
71+
dp[1] = match (s[0], s[1]) {
72+
(b'1', b'0') => [0, 1],
73+
(b'1', b'*') => [9, 9],
74+
(b'1', _) => [1, 1],
75+
(b'2', b'0') => [0, 1],
76+
(b'2', b'*') => [9, 6],
77+
(b'2', x) if x < b'7' => [1, 1],
78+
(b'2', _) => [1, 0],
79+
(b'*', b'0') => [0, 2],
80+
(b'*', b'*') => [81, 15],
81+
(b'*', x) if x < b'7' => [9, 2],
82+
(b'*', _) => [9, 1],
83+
(_, b'0') => [0, 0],
84+
(_, b'*') => [9, 0],
85+
_ => [1, 0],
86+
};
87+
}
88+
89+
for i in 2..s.len() {
90+
dp[i][0] = match s[i] {
91+
b'0' => 0,
92+
b'*' => 9 * (dp[i - 1][0] + dp[i - 1][1]),
93+
_ => dp[i - 1][0] + dp[i - 1][1],
94+
} % 1_000_000_007;
95+
96+
dp[i][1] = match (s[i - 1], s[i]) {
97+
(b'1', b'*') => 9 * (dp[i - 2][0] + dp[i - 2][1]),
98+
(b'1', _) => dp[i - 2][0] + dp[i - 2][1],
99+
(b'2', b'*') => 6 * (dp[i - 2][0] + dp[i - 2][1]),
100+
(b'2', x) if x < b'7' => dp[i - 2][0] + dp[i - 2][1],
101+
(b'*', b'*') => 15 * (dp[i - 2][0] + dp[i - 2][1]),
102+
(b'*', x) if x < b'7' => 2 * (dp[i - 2][0] + dp[i - 2][1]),
103+
(b'*', _) => dp[i - 2][0] + dp[i - 2][1],
104+
_ => 0,
105+
} % 1_000_000_007;
106+
}
107+
108+
((dp[dp.len() - 1][0] + dp[dp.len() - 1][1]) % 1_000_000_007) as i32
109+
}
110+
}
111+
```
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# 639. 解码方法 II
2+
一条包含字母 `A-Z` 的消息通过以下的方式进行了 **编码**
3+
4+
```
5+
'A' -> "1"
6+
'B' -> "2"
7+
...
8+
'Z' -> "26"
9+
```
10+
11+
**解码** 一条已编码的消息,所有的数字都必须分组,然后按原来的编码方案反向映射回字母(可能存在多种方式)。例如,`"11106"` 可以映射为:
12+
13+
* `"AAJF"` 对应分组 `(1 1 10 6)`
14+
* `"KJF"` 对应分组 `(11 10 6)`
15+
16+
注意,像 `(1 11 06)` 这样的分组是无效的,因为 `"06"` 不可以映射为 `'F'` ,因为 `"6"``"06"` 不同。
17+
18+
**除了** 上面描述的数字字母映射方案,编码消息中可能包含 `'*'` 字符,可以表示从 `'1'``'9'` 的任一数字(不包括 `'0'`)。例如,编码字符串 `"1*"` 可以表示 `"11"``"12"``"13"``"14"``"15"``"16"``"17"``"18"``"19"` 中的任意一条消息。对 `"1*"` 进行解码,相当于解码该字符串可以表示的任何编码消息。
19+
20+
给你一个字符串 `s` ,由数字和 `'*'` 字符组成,返回 **解码** 该字符串的方法 **数目**
21+
22+
由于答案数目可能非常大,返回 <code>10<sup>9</sup> + 7</code> 的 ****
23+
24+
#### 示例 1:
25+
<pre>
26+
<strong>输入:</strong> s = "*"
27+
<strong>输出:</strong> 9
28+
<strong>解释:</strong> 这一条编码消息可以表示 "1"、"2"、"3"、"4"、"5"、"6"、"7"、"8" 或 "9" 中的任意一条。
29+
可以分别解码成字符串 "A"、"B"、"C"、"D"、"E"、"F"、"G"、"H" 和 "I" 。
30+
因此,"*" 总共有 9 种解码方法。
31+
</pre>
32+
33+
#### 示例 2:
34+
<pre>
35+
<strong>输入:</strong> s = "1*"
36+
<strong>输出:</strong> 18
37+
<strong>解释:</strong> 这一条编码消息可以表示 "11"、"12"、"13"、"14"、"15"、"16"、"17"、"18" 或 "19" 中的任意一条。
38+
每种消息都可以由 2 种方法解码(例如,"11" 可以解码成 "AA" 或 "K")。
39+
因此,"1*" 共有 9 * 2 = 18 种解码方法。
40+
</pre>
41+
42+
#### 示例 3:
43+
<pre>
44+
<strong>输入:</strong> s = "2*"
45+
<strong>输出:</strong> 15
46+
<strong>解释:</strong> 这一条编码消息可以表示 "21"、"22"、"23"、"24"、"25"、"26"、"27"、"28" 或 "29" 中的任意一条。
47+
"21"、"22"、"23"、"24"、"25" 和 "26" 由 2 种解码方法,但 "27"、"28" 和 "29" 仅有 1 种解码方法。
48+
因此,"2*" 共有 (6 * 2) + (3 * 1) = 12 + 3 = 15 种解码方法。
49+
</pre>
50+
51+
#### 提示:
52+
* <code>1 <= s.length <= 10<sup>5</sup></code>
53+
* `s[i]``0 - 9` 中的一位数字或字符 `'*'`
54+
55+
## 题解 (Rust)
56+
57+
### 1. 题解
58+
```Rust
59+
impl Solution {
60+
pub fn num_decodings(s: String) -> i32 {
61+
let s = s.as_bytes();
62+
let mut dp = vec![[0_i64; 2]; s.len()];
63+
64+
match s[0] {
65+
b'0' => return 0,
66+
b'*' => dp[0][0] = 9,
67+
_ => dp[0][0] = 1,
68+
}
69+
70+
if s.len() > 1 {
71+
dp[1] = match (s[0], s[1]) {
72+
(b'1', b'0') => [0, 1],
73+
(b'1', b'*') => [9, 9],
74+
(b'1', _) => [1, 1],
75+
(b'2', b'0') => [0, 1],
76+
(b'2', b'*') => [9, 6],
77+
(b'2', x) if x < b'7' => [1, 1],
78+
(b'2', _) => [1, 0],
79+
(b'*', b'0') => [0, 2],
80+
(b'*', b'*') => [81, 15],
81+
(b'*', x) if x < b'7' => [9, 2],
82+
(b'*', _) => [9, 1],
83+
(_, b'0') => [0, 0],
84+
(_, b'*') => [9, 0],
85+
_ => [1, 0],
86+
};
87+
}
88+
89+
for i in 2..s.len() {
90+
dp[i][0] = match s[i] {
91+
b'0' => 0,
92+
b'*' => 9 * (dp[i - 1][0] + dp[i - 1][1]),
93+
_ => dp[i - 1][0] + dp[i - 1][1],
94+
} % 1_000_000_007;
95+
96+
dp[i][1] = match (s[i - 1], s[i]) {
97+
(b'1', b'*') => 9 * (dp[i - 2][0] + dp[i - 2][1]),
98+
(b'1', _) => dp[i - 2][0] + dp[i - 2][1],
99+
(b'2', b'*') => 6 * (dp[i - 2][0] + dp[i - 2][1]),
100+
(b'2', x) if x < b'7' => dp[i - 2][0] + dp[i - 2][1],
101+
(b'*', b'*') => 15 * (dp[i - 2][0] + dp[i - 2][1]),
102+
(b'*', x) if x < b'7' => 2 * (dp[i - 2][0] + dp[i - 2][1]),
103+
(b'*', _) => dp[i - 2][0] + dp[i - 2][1],
104+
_ => 0,
105+
} % 1_000_000_007;
106+
}
107+
108+
((dp[dp.len() - 1][0] + dp[dp.len() - 1][1]) % 1_000_000_007) as i32
109+
}
110+
}
111+
```
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
impl Solution {
2+
pub fn num_decodings(s: String) -> i32 {
3+
let s = s.as_bytes();
4+
let mut dp = vec![[0_i64; 2]; s.len()];
5+
6+
match s[0] {
7+
b'0' => return 0,
8+
b'*' => dp[0][0] = 9,
9+
_ => dp[0][0] = 1,
10+
}
11+
12+
if s.len() > 1 {
13+
dp[1] = match (s[0], s[1]) {
14+
(b'1', b'0') => [0, 1],
15+
(b'1', b'*') => [9, 9],
16+
(b'1', _) => [1, 1],
17+
(b'2', b'0') => [0, 1],
18+
(b'2', b'*') => [9, 6],
19+
(b'2', x) if x < b'7' => [1, 1],
20+
(b'2', _) => [1, 0],
21+
(b'*', b'0') => [0, 2],
22+
(b'*', b'*') => [81, 15],
23+
(b'*', x) if x < b'7' => [9, 2],
24+
(b'*', _) => [9, 1],
25+
(_, b'0') => [0, 0],
26+
(_, b'*') => [9, 0],
27+
_ => [1, 0],
28+
};
29+
}
30+
31+
for i in 2..s.len() {
32+
dp[i][0] = match s[i] {
33+
b'0' => 0,
34+
b'*' => 9 * (dp[i - 1][0] + dp[i - 1][1]),
35+
_ => dp[i - 1][0] + dp[i - 1][1],
36+
} % 1_000_000_007;
37+
38+
dp[i][1] = match (s[i - 1], s[i]) {
39+
(b'1', b'*') => 9 * (dp[i - 2][0] + dp[i - 2][1]),
40+
(b'1', _) => dp[i - 2][0] + dp[i - 2][1],
41+
(b'2', b'*') => 6 * (dp[i - 2][0] + dp[i - 2][1]),
42+
(b'2', x) if x < b'7' => dp[i - 2][0] + dp[i - 2][1],
43+
(b'*', b'*') => 15 * (dp[i - 2][0] + dp[i - 2][1]),
44+
(b'*', x) if x < b'7' => 2 * (dp[i - 2][0] + dp[i - 2][1]),
45+
(b'*', _) => dp[i - 2][0] + dp[i - 2][1],
46+
_ => 0,
47+
} % 1_000_000_007;
48+
}
49+
50+
((dp[dp.len() - 1][0] + dp[dp.len() - 1][1]) % 1_000_000_007) as i32
51+
}
52+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@
386386
[633][633l] |[Sum of Square Numbers][633] |![rs]
387387
[636][636l] |[Exclusive Time of Functions][636] |![rs]
388388
[637][637l] |[Average of Levels in Binary Tree][637] |![py]
389+
[639][639l] |[Decode Ways II][639] |![rs]
389390
[640][640l] |[Solve the Equation][640] |![rs]
390391
[641][641l] |[Design Circular Deque][641] |![rs]
391392
[643][643l] |[Maximum Average Subarray I][643] |![rs]
@@ -1784,6 +1785,7 @@
17841785
[633]:Problemset/0633-Sum%20of%20Square%20Numbers/README.md#633-sum-of-square-numbers
17851786
[636]:Problemset/0636-Exclusive%20Time%20of%20Functions/README.md#636-exclusive-time-of-functions
17861787
[637]:Problemset/0637-Average%20of%20Levels%20in%20Binary%20Tree/README.md#637-average-of-levels-in-binary-tree
1788+
[639]:Problemset/0639-Decode%20Ways%20II/README.md#639-decode-ways-ii
17871789
[640]:Problemset/0640-Solve%20the%20Equation/README.md#640-solve-the-equation
17881790
[641]:Problemset/0641-Design%20Circular%20Deque/README.md#641-design-circular-deque
17891791
[643]:Problemset/0643-Maximum%20Average%20Subarray%20I/README.md#643-maximum-average-subarray-i
@@ -3180,6 +3182,7 @@
31803182
[633l]:https://leetcode.com/problems/sum-of-square-numbers/
31813183
[636l]:https://leetcode.com/problems/exclusive-time-of-functions/
31823184
[637l]:https://leetcode.com/problems/average-of-levels-in-binary-tree/
3185+
[639l]:https://leetcode.com/problems/decode-ways-ii/
31833186
[640l]:https://leetcode.com/problems/solve-the-equation/
31843187
[641l]:https://leetcode.com/problems/design-circular-deque/
31853188
[643l]:https://leetcode.com/problems/maximum-average-subarray-i/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@
386386
[633][633l] |[平方数之和][633] |![rs]
387387
[636][636l] |[函数的独占时间][636] |![rs]
388388
[637][637l] |[二叉树的层平均值][637] |![py]
389+
[639][639l] |[解码方法 II][639] |![rs]
389390
[640][640l] |[求解方程][640] |![rs]
390391
[641][641l] |[设计循环双端队列][641] |![rs]
391392
[643][643l] |[子数组最大平均数 I][643] |![rs]
@@ -1784,6 +1785,7 @@
17841785
[633]:Problemset/0633-Sum%20of%20Square%20Numbers/README_CN.md#633-平方数之和
17851786
[636]:Problemset/0636-Exclusive%20Time%20of%20Functions/README_CN.md#636-函数的独占时间
17861787
[637]:Problemset/0637-Average%20of%20Levels%20in%20Binary%20Tree/README_CN.md#637-二叉树的层平均值
1788+
[639]:Problemset/0639-Decode%20Ways%20II/README_CN.md#639-解码方法-ii
17871789
[640]:Problemset/0640-Solve%20the%20Equation/README_CN.md#640-求解方程
17881790
[641]:Problemset/0641-Design%20Circular%20Deque/README_CN.md#641-设计循环双端队列
17891791
[643]:Problemset/0643-Maximum%20Average%20Subarray%20I/README_CN.md#643-子数组最大平均数-i
@@ -3180,6 +3182,7 @@
31803182
[633l]:https://leetcode.cn/problems/sum-of-square-numbers/
31813183
[636l]:https://leetcode.cn/problems/exclusive-time-of-functions/
31823184
[637l]:https://leetcode.cn/problems/average-of-levels-in-binary-tree/
3185+
[639l]:https://leetcode.cn/problems/decode-ways-ii/
31833186
[640l]:https://leetcode.cn/problems/solve-the-equation/
31843187
[641l]:https://leetcode.cn/problems/design-circular-deque/
31853188
[643l]:https://leetcode.cn/problems/maximum-average-subarray-i/

0 commit comments

Comments
 (0)