{"id":757,"date":"2017-11-09T21:26:45","date_gmt":"2017-11-10T05:26:45","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=757"},"modified":"2018-04-19T08:35:07","modified_gmt":"2018-04-19T15:35:07","slug":"leetcode-91-decode-ways","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-91-decode-ways\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 91. Decode Ways"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 91. Decode Ways - \u5237\u9898\u627e\u5de5\u4f5c EP103\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/OjEHST4SXfE?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>\u9898\u76ee\u5927\u610f\uff1a<\/p>\n<p>\u7ed9\u4f60\u4e00\u4e2a\u52a0\u5bc6\u7684\u6570\u5b57\u5b57\u7b26\u4e32\uff0c\u95ee\u4f60\u4e00\u5171\u6709\u591a\u5c11\u79cd\u4e0d\u540c\u7684\u89e3\u5bc6\u65b9\u5f0f\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<div class=\"question-description\">\n<p>A message containing letters from\u00a0<code>A-Z<\/code>\u00a0is being encoded to numbers using the following mapping:<\/p>\n<p class=\"\">&#8216;A&#8217; -&gt; 1<\/p>\n<p class=\"\">&#8216;B&#8217; -&gt; 2<\/p>\n<p class=\"\">&#8230;<\/p>\n<p class=\"\">&#8216;Z&#8217; -&gt; 26<\/p>\n<p>Given an encoded message containing digits, determine the total number of ways to decode it.<\/p>\n<p>For example,<br \/>\nGiven encoded message\u00a0<code>\"12\"<\/code>, it could be decoded as\u00a0<code>\"AB\"<\/code>\u00a0(1 2) or\u00a0<code>\"L\"<\/code>\u00a0(12).<\/p>\n<p>The number of ways decoding\u00a0<code>\"12\"<\/code>\u00a0is 2.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"question-description\">\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>Dynamic Programming<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/91-ep103-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-766 size-full\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/91-ep103-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/91-ep103-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/91-ep103-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/91-ep103-1-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/91-ep103-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\/91-ep103-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-761\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/91-ep103-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/91-ep103-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/91-ep103-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/91-ep103-2-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/91-ep103-2-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<\/div>\n<div id=\"interviewed-div\"><\/div>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++<\/p>\n<p>Time complexity: O(n^2)<\/p>\n<p>Space complexity: O(n^2)<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 6 ms\r\nclass Solution {\r\npublic:\r\n    int numDecodings(string s) {\r\n        if(s.length() == 0) return 0;        \r\n        m_ways[\"\"] = 1;        \r\n        return ways(s);\r\n    }\r\n\r\nprivate:\r\n    int ways(const string&amp; s) {\r\n        if (m_ways.count(s)) return m_ways[s];\r\n        if (s[0] == '0') return 0;\r\n        if (s.length() == 1) return 1;        \r\n        \r\n        int w = ways(s.substr(1));        \r\n        const int prefix = stoi(s.substr(0, 2));\r\n        \r\n        if (prefix &lt;= 26)\r\n             w += ways(s.substr(2));        \r\n        \r\n        m_ways[s] = w;        \r\n        return w;\r\n    }\r\n\r\n    unordered_map&lt;string, int&gt; m_ways;\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>C++<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 3 ms\r\nclass Solution {\r\npublic:\r\n    int numDecodings(string s) {\r\n        if(s.length() == 0) return 0;\r\n        return ways(s, 0, s.length() - 1);\r\n    }\r\n\r\nprivate:    \r\n    int ways(const string&amp; s, int l, int r) {        \r\n        if (m_ways.count(l)) return m_ways[l];\r\n        if (s[l] == '0') return 0;\r\n        if (l &gt;= r) return 1; \/\/ Single digit or empty.\r\n        \r\n        int w = ways(s, l + 1, r);\r\n        const int prefix = (s[l] - '0') * 10 + (s[l + 1] - '0');\r\n        \r\n        if (prefix &lt;= 26)\r\n             w += ways(s, l + 2, r);\r\n        \r\n        m_ways[l] = w;\r\n        return w;\r\n    }\r\n    \r\n    \/\/ Use l as key.\r\n    unordered_map&lt;int, int&gt; m_ways;\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>C++<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\r\npublic:\r\n    int numDecodings(string s) {\r\n        if (s.empty() || s[0] == '0') return 0;\r\n        if (s.length() == 1) return 1;\r\n        \r\n        const int n = s.length();\r\n        int w1 = 1;\r\n        int w2 = 1;        \r\n        for (int i = 1; i &lt; n; ++i) {\r\n            int w = 0;\r\n            if (!isValid(s[i]) &amp;&amp; !isValid(s[i - 1], s[i])) return 0;\r\n            if (isValid(s[i])) w = w1;\r\n            if (isValid(s[i - 1], s[i])) w += w2;\r\n            w2 = w1;\r\n            w1 = w;\r\n        }\r\n        return w1;\r\n    }\r\nprivate:\r\n    bool isValid(const char c) { return c != '0'; }\r\n    bool isValid(const char c1, const char c2) { \r\n        const int num = (c1 - '0')*10 + (c2 - '0');\r\n        return num &gt;= 10 &amp;&amp; num &lt;= 26;\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-639-decode-ways-ii\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 639. Decode Ways II &#8211; \u82b1\u82b1\u9171<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a \u7ed9\u4f60\u4e00\u4e2a\u52a0\u5bc6\u7684\u6570\u5b57\u5b57\u7b26\u4e32\uff0c\u95ee\u4f60\u4e00\u5171\u6709\u591a\u5c11\u79cd\u4e0d\u540c\u7684\u89e3\u5bc6\u65b9\u5f0f\u3002 Problem: A message containing letters from\u00a0A-Z\u00a0is being encoded to numbers using the following mapping: &#8216;A&#8217; -&gt; 1 &#8216;B&#8217; -&gt; 2 &#8230; &#8216;Z&#8217; -&gt;&#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,164],"tags":[8,18],"class_list":["post-757","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","category-medium","tag-counting","tag-dp","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/757","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=757"}],"version-history":[{"count":13,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/757\/revisions"}],"predecessor-version":[{"id":2622,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/757\/revisions\/2622"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}