|
| 1 | +--- |
| 2 | +id: decode-ways |
| 3 | +title: Decode Ways |
| 4 | +sidebar_label: 0091 - Decode Ways |
| 5 | +tags: |
| 6 | + - dyp |
| 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 `dyp` array where `dyp[i]` represents the number of ways to decode the substring `s[0:i]`. |
| 68 | +3. Set `dyp[0]` to 1 (base case for the empty string). |
| 69 | +4. Set `dyp[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 `dyp[i-1]` to `dyp[i]`. |
| 72 | + - Check if the two-digit number formed with the previous character is valid (between "10" and "26"). If it does, add `dyp[i-2]` to `dyp[i]`. |
| 73 | +6. The result is in `dyp[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 | + dyp = array of size (n + 1) initialized to 0 |
| 84 | + dyp[0] = 1 |
| 85 | + dyp[1] = 1 if s[0] != '0' else 0 |
| 86 | +
|
| 87 | + for i from 2 to n: |
| 88 | + if s[i-1] != '0': |
| 89 | + dyp[i] += dyp[i-1] |
| 90 | +
|
| 91 | + twodigit = integer value of s[i-2:i] |
| 92 | + if 10 <= twodigit <= 26: |
| 93 | + dyp[i] += dyp[i-2] |
| 94 | +
|
| 95 | + return dyp[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 | + dyp = [0] * (n + 1) |
| 108 | + dyp[0] = 1 |
| 109 | + dyp[1] = 1 if s[0] != '0' else 0 |
| 110 | + |
| 111 | + for i in range(2, n + 1): |
| 112 | + if s[i - 1] != '0': |
| 113 | + dyp[i] += dyp[i - 1] |
| 114 | + |
| 115 | + twodigit = int(s[i - 2:i]) |
| 116 | + if 10 <= twodigit <= 26: |
| 117 | + dyp[i] += dyp[i - 2] |
| 118 | + |
| 119 | + return dyp[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> dyp(n + 1, 0); |
| 132 | + dyp[0] = 1; |
| 133 | + dyp[1] = s[0] != '0' ? 1 : 0; |
| 134 | + |
| 135 | + for (int i = 2; i <= n; ++i) { |
| 136 | + if (s[i - 1] != '0') { |
| 137 | + dyp[i] += dyp[i - 1]; |
| 138 | + } |
| 139 | + |
| 140 | + int twodigit = stoi(s.substr(i - 2, 2)); |
| 141 | + if (10 <= twodigit && twodigit <= 26) { |
| 142 | + dyp[i] += dyp[i - 2]; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return dyp[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[] dyp = new int[n + 1]; |
| 163 | + dyp[0] = 1; |
| 164 | + dyp[1] = s.charAt(0) != '0' ? 1 : 0; |
| 165 | + |
| 166 | + for (int i = 2; i <= n; ++i) { |
| 167 | + if (s.charAt(i - 1) != '0') { |
| 168 | + dyp[i] += dyp[i - 1]; |
| 169 | + } |
| 170 | + |
| 171 | + int twoDigit = Integer.parseInt(s.substring(i - 2, i)); |
| 172 | + if (10 <= twoDigit && twoDigit <= 26) { |
| 173 | + dyp[i] += dyp[i - 2]; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return dyp[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 dyp = new Array(n + 1).fill(0); |
| 190 | + dyp[0] = 1; |
| 191 | + dyp[1] = s[0] !== "0" ? 1 : 0; |
| 192 | + |
| 193 | + for (let i = 2; i <= n; i++) { |
| 194 | + if (s[i - 1] !== "0") { |
| 195 | + dyp[i] += dyp[i - 1]; |
| 196 | + } |
| 197 | + |
| 198 | + let twoDigit = parseInt(s.substring(i - 2, i), 10); |
| 199 | + if (10 <= twoDigit && twoDigit <= 26) { |
| 200 | + dyp[i] += dyp[i - 2]; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + return dyp[n]; |
| 205 | +}; |
| 206 | +``` |
0 commit comments