{"id":831,"date":"2017-11-18T19:53:39","date_gmt":"2017-11-19T03:53:39","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=831"},"modified":"2018-04-19T08:31:49","modified_gmt":"2018-04-19T15:31:49","slug":"leetcode-639-decode-ways-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-639-decode-ways-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 639. Decode Ways II"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 639. Decode Ways II - \u5237\u9898\u627e\u5de5\u4f5c EP110\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/65j9zS-YWZo?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>A message containing letters from\u00a0<code>A-Z<\/code>\u00a0is being encoded to numbers using the following mapping way:<\/p>\n<pre class=\"lang:sh decode:true\">'A' -&gt; 1\r\n'B' -&gt; 2\r\n...\r\n'Z' -&gt; 26\r\n<\/pre>\n<p>Beyond that, now the encoded string can also contain the character &#8216;*&#8217;, which can be treated as one of the numbers from 1 to 9.<\/p>\n<p>Given the encoded message containing digits and the character &#8216;*&#8217;, return the total number of ways to decode it.<\/p>\n<p>Also, since the answer may be very large, you should return the output mod 10<sup>9<\/sup>\u00a0+ 7.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: \"*\"\r\nOutput: 9\r\nExplanation: The encoded message can be decoded to the string:\r\n\"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\", \"I\".\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: \"1*\"\r\nOutput: 9 + 9 = 18\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The length of the input string will fit in range [1, 10<sup>5<\/sup>].<\/li>\n<li>The input string will only contain the character &#8216;*&#8217; and digits &#8216;0&#8217; &#8211; &#8216;9&#8217;.<\/li>\n<\/ol>\n<p><strong>Idea:<\/strong><\/p>\n<p>DP<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/639-ep110-1-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-843 size-full\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/639-ep110-1-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/639-ep110-1-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/639-ep110-1-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/639-ep110-1-1-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/639-ep110-1-1-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/639-ep110-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-836\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/639-ep110-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/639-ep110-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/639-ep110-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/639-ep110-2-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/639-ep110-2-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 58 ms\r\nclass Solution {\r\npublic:\r\n    int numDecodings(string s) {\r\n        if (s.empty()) return 0;        \r\n        \/\/           dp[-1]  dp[0]\r\n        long dp[2] = {1, ways(s[0])};\r\n        for (int i = 1; i &lt; s.length(); ++i) {\r\n            long dp_i = ways(s[i]) * dp[1] + ways(s[i - 1], s[i]) * dp[0];\r\n            dp_i %= kMod;\r\n            dp[0] = dp[1];\r\n            dp[1] = dp_i;\r\n        }\r\n        return dp[1];\r\n    }\r\nprivate:\r\n    static constexpr int kMod = 1000000007;    \r\n    \r\n    int ways(char c) {\r\n        if (c == '0') return 0;\r\n        if (c == '*') return 9;\r\n        return 1;\r\n    }\r\n    \r\n    int ways(char c1, char c2) {\r\n        if (c1 == '*' &amp;&amp; c2 == '*') \r\n            return 15;\r\n        if (c1 == '*') {\r\n          return (c2 &gt;= '0' &amp;&amp; c2 &lt;= '6') ? 2 : 1;\r\n        } else if (c2 == '*') {\r\n            switch (c1) {\r\n                case '1': return 9;\r\n                case '2': return 6;\r\n                default: return 0;\r\n            }\r\n        } else {\r\n            int prefix = (c1 - '0') * 10 + (c2 - '0');\r\n            return prefix &gt;= 10 &amp;&amp; prefix &lt;= 26;\r\n        }        \r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-91-decode-ways\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 91. Decode Ways<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem: A message containing letters from\u00a0A-Z\u00a0is being encoded to numbers using the following mapping way: &#8216;A&#8217; -&gt; 1 &#8216;B&#8217; -&gt; 2 &#8230; &#8216;Z&#8217; -&gt; 26&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,165],"tags":[8,155,4,154],"class_list":["post-831","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","category-hard","tag-counting","tag-decode","tag-string","tag-ways","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=831"}],"version-history":[{"count":10,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/831\/revisions"}],"predecessor-version":[{"id":2578,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/831\/revisions\/2578"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}