Class Solution

java.lang.Object
g0601_0700.s0639_decode_ways_ii.Solution

public class Solution extends Object
639 - Decode Ways II.<p>Hard</p> <p>A message containing letters from <code>A-Z</code> can be <strong>encoded</strong> into numbers using the following mapping:</p> <p>&lsquo;A&rsquo; -> &ldquo;1&rdquo; &lsquo;B&rsquo; -> &ldquo;2&rdquo; &hellip; &lsquo;Z&rsquo; -> &ldquo;26&rdquo;</p> <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>&quot;11106&quot;</code> can be mapped into:</p> <ul> <li><code>&quot;AAJF&quot;</code> with the grouping <code>(1 1 10 6)</code></li> <li><code>&quot;KJF&quot;</code> with the grouping <code>(11 10 6)</code></li> </ul> <p>Note that the grouping <code>(1 11 06)</code> is invalid because <code>&quot;06&quot;</code> cannot be mapped into <code>'F'</code> since <code>&quot;6&quot;</code> is different from <code>&quot;06&quot;</code>.</p> <p><strong>In addition</strong> to the mapping above, an encoded message may contain the <code>'*'</code> character, which can represent any digit from <code>'1'</code> to <code>'9'</code> (<code>'0'</code> is excluded). For example, the encoded message <code>&quot;1*&quot;</code> may represent any of the encoded messages <code>&quot;11&quot;</code>, <code>&quot;12&quot;</code>, <code>&quot;13&quot;</code>, <code>&quot;14&quot;</code>, <code>&quot;15&quot;</code>, <code>&quot;16&quot;</code>, <code>&quot;17&quot;</code>, <code>&quot;18&quot;</code>, or <code>&quot;19&quot;</code>. Decoding <code>&quot;1*&quot;</code> is equivalent to decoding <strong>any</strong> of the encoded messages it can represent.</p> <p>Given a string <code>s</code> consisting of digits and <code>'*'</code> characters, return <em>the <strong>number</strong> of ways to <strong>decode</strong> it</em>.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;*&rdquo;</p> <p><strong>Output:</strong> 9</p> <p><strong>Explanation:</strong> The encoded message can represent any of the encoded messages &ldquo;1&rdquo;, &ldquo;2&rdquo;, &ldquo;3&rdquo;, &ldquo;4&rdquo;, &ldquo;5&rdquo;, &ldquo;6&rdquo;, &ldquo;7&rdquo;, &ldquo;8&rdquo;, or &ldquo;9&rdquo;. Each of these can be decoded to the strings &ldquo;A&rdquo;, &ldquo;B&rdquo;, &ldquo;C&rdquo;, &ldquo;D&rdquo;, &ldquo;E&rdquo;, &ldquo;F&rdquo;, &ldquo;G&rdquo;, &ldquo;H&rdquo;, and &ldquo;I&rdquo; respectively. Hence, there are a total of 9 ways to decode &ldquo;*&rdquo;.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;1*&rdquo;</p> <p><strong>Output:</strong> 18</p> <p><strong>Explanation:</strong> The encoded message can represent any of the encoded messages &ldquo;11&rdquo;, &ldquo;12&rdquo;, &ldquo;13&rdquo;, &ldquo;14&rdquo;, &ldquo;15&rdquo;, &ldquo;16&rdquo;, &ldquo;17&rdquo;, &ldquo;18&rdquo;, or &ldquo;19&rdquo;. Each of these encoded messages have 2 ways to be decoded (e.g. &ldquo;11&rdquo; can be decoded to &ldquo;AA&rdquo; or &ldquo;K&rdquo;). Hence, there are a total of 9 * 2 = 18 ways to decode &ldquo;1*&rdquo;.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;2*&rdquo;</p> <p><strong>Output:</strong> 15</p> <p><strong>Explanation:</strong> The encoded message can represent any of the encoded messages &ldquo;21&rdquo;, &ldquo;22&rdquo;, &ldquo;23&rdquo;, &ldquo;24&rdquo;, &ldquo;25&rdquo;, &ldquo;26&rdquo;, &ldquo;27&rdquo;, &ldquo;28&rdquo;, or &ldquo;29&rdquo;. &ldquo;21&rdquo;, &ldquo;22&rdquo;, &ldquo;23&rdquo;, &ldquo;24&rdquo;, &ldquo;25&rdquo;, and &ldquo;26&rdquo; have 2 ways of being decoded, but &ldquo;27&rdquo;, &ldquo;28&rdquo;, and &ldquo;29&rdquo; only have 1 way. Hence, there are a total of (6 * 2) + (3 * 1) = 12 + 3 = 15 ways to decode &ldquo;2*&rdquo;.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 10<sup>5</sup></code></li> <li><code>s[i]</code> is a digit or <code>'*'</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • numDecodings

      public int numDecodings(String s)