Class Solution

java.lang.Object
g0001_0100.s0091_decode_ways.Solution

public class Solution extends Object
91 - Decode Ways.<p>Medium</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>Given a string <code>s</code> containing only digits, return <em>the <strong>number</strong> of ways to <strong>decode</strong> it</em>.</p> <p>The answer is guaranteed to fit in a <strong>32-bit</strong> integer.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;12&rdquo;</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> &ldquo;12&rdquo; could be decoded as &ldquo;AB&rdquo; (1 2) or &ldquo;L&rdquo; (12).</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;226&rdquo;</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> &ldquo;226&rdquo; could be decoded as &ldquo;BZ&rdquo; (2 26), &ldquo;VF&rdquo; (22 6), or &ldquo;BBF&rdquo; (2 2 6).</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;0&rdquo;</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> There is no character that is mapped to a number starting with 0. The only valid mappings with 0 are &lsquo;J&rsquo; -> &ldquo;10&rdquo; and &lsquo;T&rsquo; -> &ldquo;20&rdquo;, neither of which start with 0. Hence, there are no valid ways to decode this since all digits need to be mapped.</p> <p><strong>Example 4:</strong></p> <p><strong>Input:</strong> s = &ldquo;06&rdquo;</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> &ldquo;06&rdquo; cannot be mapped to &ldquo;F&rdquo; because of the leading zero (&ldquo;6&rdquo; is different from &ldquo;06&rdquo;).</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 100</code></li> <li><code>s</code> contains only digits and may contain leading zero(s).</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • numDecodings

      public int numDecodings(String s)