You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/0000-0099/0091.Decode Ways/README_EN.md
+42-28Lines changed: 42 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -17,52 +17,66 @@ tags:
17
17
18
18
<!-- description:start -->
19
19
20
-
<p>A message containing letters from <code>A-Z</code> can be <strong>encoded</strong> into numbers using the following mapping:</p>
20
+
<p>You have intercepted a secret message encoded as a string of numbers. The message is <strong>decoded</strong> via the following mapping:</p>
21
21
22
-
<pre>
23
-
'A' ->"1"
24
-
'B' ->"2"
25
-
...
26
-
'Z' ->"26"
27
-
</pre>
22
+
<p><code>"1" ->'A'<br />
23
+
"2" ->'B'<br />
24
+
...<br />
25
+
"25" ->'Y'<br />
26
+
"26" ->'Z'</code></p>
28
27
29
-
<p>To <strong>decode</strong> 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, <code>"11106"</code> can be mapped into:</p>
28
+
<p>However, while decoding the message, you realize that there are many different ways you can decode the message because some codes are contained in other codes (<code>"2"</code> and <code>"5"</code> vs <code>"25"</code>).</p>
29
+
30
+
<p>For example, <code>"11106"</code> can be decoded into:</p>
30
31
31
32
<ul>
32
-
<li><code>"AAJF"</code> with the grouping <code>(1 1 10 6)</code></li>
33
-
<li><code>"KJF"</code> with the grouping <code>(11 10 6)</code></li>
33
+
<li><code>"AAJF"</code> with the grouping <code>(1, 1, 10, 6)</code></li>
34
+
<li><code>"KJF"</code> with the grouping <code>(11, 10, 6)</code></li>
35
+
<li>The grouping <code>(1, 11, 06)</code> is invalid because <code>"06"</code> is not a valid code (only <code>"6"</code> is valid).</li>
34
36
</ul>
35
37
36
-
<p>Note that the grouping <code>(1 11 06)</code> is invalid because <code>"06"</code> cannot be mapped into <code>'F'</code> since <code>"6"</code> is different from <code>"06"</code>.</p>
37
-
38
-
<p>Given a string <code>s</code> containing only digits, return <em>the <strong>number</strong> of ways to <strong>decode</strong> it</em>.</p>
38
+
<p>Note: there may be strings that are impossible to decode.<br />
39
+
<br />
40
+
Given a string s containing only digits, return the <strong>number of ways</strong> to <strong>decode</strong> it. If the entire string cannot be decoded in any valid way, return <code>0</code>.</p>
39
41
40
42
<p>The test cases are generated so that the answer fits in a <strong>32-bit</strong> integer.</p>
41
43
42
44
<p> </p>
43
45
<p><strongclass="example">Example 1:</strong></p>
44
46
45
-
<pre>
46
-
<strong>Input:</strong> s = "12"
47
-
<strong>Output:</strong> 2
48
-
<strong>Explanation:</strong> "12" could be decoded as "AB" (1 2) or "L" (12).
<p>"226" could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).</p>
67
+
</div>
58
68
59
69
<p><strongclass="example">Example 3:</strong></p>
60
70
61
-
<pre>
62
-
<strong>Input:</strong> s = "06"
63
-
<strong>Output:</strong> 0
64
-
<strong>Explanation:</strong> "06" cannot be mapped to "F" because of the leading zero ("6" is different from "06").
<p>"06" cannot be mapped to "F" because of the leading zero ("6" is different from "06"). In this case, the string is not a valid encoding, so return 0.</p>
0 commit comments