<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ways Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/ways/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/ways/</link>
	<description></description>
	<lastBuildDate>Thu, 19 Apr 2018 15:31:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>ways Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/ways/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 639. Decode Ways II</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-639-decode-ways-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-639-decode-ways-ii/#comments</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 19 Nov 2017 03:53:39 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[Hard]]></category>
		<category><![CDATA[counting]]></category>
		<category><![CDATA[decode]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[ways]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=831</guid>

					<description><![CDATA[<p>Problem: A message containing letters from A-Z is being encoded to numbers using the following mapping way: [crayon-663ca7cdcb329297468748/] Beyond that, now the encoded string can also contain&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-639-decode-ways-ii/">花花酱 LeetCode 639. Decode Ways II</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/65j9zS-YWZo?feature=oembed" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>A message containing letters from <code>A-Z</code> is being encoded to numbers using the following mapping way:</p><pre class="crayon-plain-tag">'A' -&gt; 1
'B' -&gt; 2
...
'Z' -&gt; 26</pre><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>
<p>Given the encoded message containing digits and the character &#8216;*&#8217;, return the total number of ways to decode it.</p>
<p>Also, since the answer may be very large, you should return the output mod 10<sup>9</sup> + 7.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input: &quot;*&quot;
Output: 9
Explanation: The encoded message can be decoded to the string:
&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;, &quot;G&quot;, &quot;H&quot;, &quot;I&quot;.</pre><p><b>Example 2:</b></p><pre class="crayon-plain-tag">Input: &quot;1*&quot;
Output: 9 + 9 = 18</pre><p><b>Note:</b></p>
<ol>
<li>The length of the input string will fit in range [1, 10<sup>5</sup>].</li>
<li>The input string will only contain the character &#8216;*&#8217; and digits &#8216;0&#8217; &#8211; &#8216;9&#8217;.</li>
</ol>
<p><strong>Idea:</strong></p>
<p>DP</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/639-ep110-1-1.png"><img 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="(max-width: 960px) 100vw, 960px" /></a></p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/639-ep110-2.png"><img 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="(max-width: 960px) 100vw, 960px" /></a></p>
<p><strong>Solution:</strong></p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 58 ms
class Solution {
public:
    int numDecodings(string s) {
        if (s.empty()) return 0;        
        //           dp[-1]  dp[0]
        long dp[2] = {1, ways(s[0])};
        for (int i = 1; i &lt; s.length(); ++i) {
            long dp_i = ways(s[i]) * dp[1] + ways(s[i - 1], s[i]) * dp[0];
            dp_i %= kMod;
            dp[0] = dp[1];
            dp[1] = dp_i;
        }
        return dp[1];
    }
private:
    static constexpr int kMod = 1000000007;    
    
    int ways(char c) {
        if (c == '0') return 0;
        if (c == '*') return 9;
        return 1;
    }
    
    int ways(char c1, char c2) {
        if (c1 == '*' &amp;&amp; c2 == '*') 
            return 15;
        if (c1 == '*') {
          return (c2 &gt;= '0' &amp;&amp; c2 &lt;= '6') ? 2 : 1;
        } else if (c2 == '*') {
            switch (c1) {
                case '1': return 9;
                case '2': return 6;
                default: return 0;
            }
        } else {
            int prefix = (c1 - '0') * 10 + (c2 - '0');
            return prefix &gt;= 10 &amp;&amp; prefix &lt;= 26;
        }        
    }
};</pre><p>&nbsp;</p>
<p><strong>Related Problems:</strong></p>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-91-decode-ways/">[解题报告] LeetCode 91. Decode Ways</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-639-decode-ways-ii/">花花酱 LeetCode 639. Decode Ways II</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-639-decode-ways-ii/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
