Skip to content

Commit b6dffb4

Browse files
committed
Added 91
1 parent 8952465 commit b6dffb4

File tree

1 file changed

+206
-0
lines changed

1 file changed

+206
-0
lines changed
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
---
2+
id: decode-ways
3+
title: Decode Ways
4+
sidebar_label: 0091 - Decode Ways
5+
tags:
6+
- DP
7+
- Leetcode
8+
9+
description: "This is a solution to the Decode Ways on LeetCode."
10+
---
11+
12+
## Problem Statement
13+
14+
A message containing letters from A-Z can be encoded into numbers using the following mapping:
15+
16+
```
17+
'A' -> "1"
18+
'B' -> "2"
19+
...
20+
'Z' -> "26"
21+
```
22+
23+
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:
24+
25+
"AAJF" with the grouping (1 1 10 6)
26+
"KJF" with the grouping (11 10 6)
27+
Note that the grouping (1 11 06) is invalid because "06" cannot be mapped into 'F' since "6" is different from "06".
28+
29+
Given a string s containing only digits, return the number of ways to decode it.
30+
31+
The test cases are generated so that the answer fits in a 32-bit integer.
32+
33+
### Examples
34+
35+
**Example 1:**
36+
37+
```
38+
Input: s = "12"
39+
Output: 2
40+
Explanation: "12" could be decoded as "AB" (1 2) or "L" (12).
41+
```
42+
43+
**Example 2:**
44+
45+
```
46+
Input: s = "226"
47+
Output: 3
48+
Explanation: "226" could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
49+
```
50+
51+
**Example 3:**
52+
53+
```
54+
Input: s = "06"
55+
Output: 0
56+
Explanation: "06" cannot be mapped to "F" because of the leading zero ("6" is different from "06").
57+
```
58+
59+
Constraints:
60+
61+
- $1 <= s.length <= 100$
62+
- s contains only digits and may contain leading zero(s).
63+
64+
### Algorithm
65+
66+
1. If the string `s` is empty or starts with '0', return 0 as it cannot be decoded.
67+
2. Initialize a `dp` array where `dp[i]` represents the number of ways to decode the substring `s[0:i]`.
68+
3. Set `dp[0]` to 1 (base case for the empty string).
69+
4. Set `dp[1]` based on the first character of the string (1 if the first character is not '0', otherwise 0).
70+
5. Iterate through the string from the second character to the end:
71+
- For each character, check if it forms a valid single-digit number (between '1' and '9'). If it does, add `dp[i-1]` to `dp[i]`.
72+
- Check if the two-digit number formed with the previous character is valid (between "10" and "26"). If it does, add `dp[i-2]` to `dp[i]`.
73+
6. The result is in `dp[n]`, where `n` is the length of the string.
74+
75+
### Pseudocode
76+
77+
```
78+
function decode(s):
79+
if s is empty or s[0] is '0':
80+
return 0
81+
82+
n = length of s
83+
dp = array of size (n + 1) initialized to 0
84+
dp[0] = 1
85+
dp[1] = 1 if s[0] != '0' else 0
86+
87+
for i from 2 to n:
88+
if s[i-1] != '0':
89+
dp[i] += dp[i-1]
90+
91+
two_digit = integer value of s[i-2:i]
92+
if 10 <= two_digit <= 26:
93+
dp[i] += dp[i-2]
94+
95+
return dp[n]
96+
```
97+
98+
### Python
99+
100+
```python
101+
class Solution:
102+
def decode(self, s: str) -> int:
103+
if not s or s[0] == '0':
104+
return 0
105+
106+
n = len(s)
107+
dp = [0] * (n + 1)
108+
dp[0] = 1
109+
dp[1] = 1 if s[0] != '0' else 0
110+
111+
for i in range(2, n + 1):
112+
if s[i - 1] != '0':
113+
dp[i] += dp[i - 1]
114+
115+
two_digit = int(s[i - 2:i])
116+
if 10 <= two_digit <= 26:
117+
dp[i] += dp[i - 2]
118+
119+
return dp[n]
120+
```
121+
122+
### C++
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
int decode(string s) {
128+
if (s.empty() || s[0] == '0') return 0;
129+
130+
int n = s.size();
131+
vector<int> dp(n + 1, 0);
132+
dp[0] = 1;
133+
dp[1] = s[0] != '0' ? 1 : 0;
134+
135+
for (int i = 2; i <= n; ++i) {
136+
if (s[i - 1] != '0') {
137+
dp[i] += dp[i - 1];
138+
}
139+
140+
int two_digit = stoi(s.substr(i - 2, 2));
141+
if (10 <= two_digit && two_digit <= 26) {
142+
dp[i] += dp[i - 2];
143+
}
144+
}
145+
146+
return dp[n];
147+
}
148+
};
149+
150+
```
151+
152+
### Java
153+
154+
```java
155+
class Solution {
156+
public int decode(String s) {
157+
if (s == null || s.length() == 0 || s.charAt(0) == '0') {
158+
return 0;
159+
}
160+
161+
int n = s.length();
162+
int[] dp = new int[n + 1];
163+
dp[0] = 1;
164+
dp[1] = s.charAt(0) != '0' ? 1 : 0;
165+
166+
for (int i = 2; i <= n; ++i) {
167+
if (s.charAt(i - 1) != '0') {
168+
dp[i] += dp[i - 1];
169+
}
170+
171+
int twoDigit = Integer.parseInt(s.substring(i - 2, i));
172+
if (10 <= twoDigit && twoDigit <= 26) {
173+
dp[i] += dp[i - 2];
174+
}
175+
}
176+
177+
return dp[n];
178+
}
179+
}
180+
```
181+
182+
### JavaScript
183+
184+
```javascript
185+
var decode = function (s) {
186+
if (!s || s[0] === "0") return 0;
187+
188+
let n = s.length;
189+
let dp = new Array(n + 1).fill(0);
190+
dp[0] = 1;
191+
dp[1] = s[0] !== "0" ? 1 : 0;
192+
193+
for (let i = 2; i <= n; i++) {
194+
if (s[i - 1] !== "0") {
195+
dp[i] += dp[i - 1];
196+
}
197+
198+
let twoDigit = parseInt(s.substring(i - 2, i), 10);
199+
if (10 <= twoDigit && twoDigit <= 26) {
200+
dp[i] += dp[i - 2];
201+
}
202+
}
203+
204+
return dp[n];
205+
};
206+
```

0 commit comments

Comments
 (0)