<?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>decode Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/decode/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/decode/</link>
	<description></description>
	<lastBuildDate>Tue, 10 May 2022 14:48:29 +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>decode Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/decode/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2266. Count Number of Texts</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-2266-count-number-of-texts/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-2266-count-number-of-texts/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 10 May 2022 14:47:44 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[decode]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9731</guid>

					<description><![CDATA[<p>Alice is texting Bob using her phone. The&#160;mapping&#160;of digits to letters is shown in the figure below. In order to&#160;add&#160;a letter, Alice has to&#160;press&#160;the key&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-2266-count-number-of-texts/">花花酱 LeetCode 2266. Count Number of Texts</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>Alice is texting Bob using her phone. The&nbsp;<strong>mapping</strong>&nbsp;of digits to letters is shown in the figure below.</p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" alt=""/></figure>



<p>In order to&nbsp;<strong>add</strong>&nbsp;a letter, Alice has to&nbsp;<strong>press</strong>&nbsp;the key of the corresponding digit&nbsp;<code>i</code>&nbsp;times, where&nbsp;<code>i</code>&nbsp;is the position of the letter in the key.</p>



<ul><li>For example, to add the letter&nbsp;<code>'s'</code>, Alice has to press&nbsp;<code>'7'</code>&nbsp;four times. Similarly, to add the letter&nbsp;<code>'k'</code>, Alice has to press&nbsp;<code>'5'</code>&nbsp;twice.</li><li>Note that the digits&nbsp;<code>'0'</code>&nbsp;and&nbsp;<code>'1'</code>&nbsp;do not map to any letters, so Alice&nbsp;<strong>does not</strong>&nbsp;use them.</li></ul>



<p>However, due to an error in transmission, Bob did not receive Alice&#8217;s text message but received a&nbsp;<strong>string of pressed keys</strong>&nbsp;instead.</p>



<ul><li>For example, when Alice sent the message&nbsp;<code>"bob"</code>, Bob received the string&nbsp;<code>"2266622"</code>.</li></ul>



<p>Given a string&nbsp;<code>pressedKeys</code>&nbsp;representing the string received by Bob, return&nbsp;<em>the&nbsp;<strong>total number of possible text messages</strong>&nbsp;Alice could have sent</em>.</p>



<p>Since the answer may be very large, return it&nbsp;<strong>modulo</strong>&nbsp;<code>10<sup>9</sup>&nbsp;+ 7</code>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> pressedKeys = "22233"
<strong>Output:</strong> 8
<strong>Explanation:</strong>
The possible text messages Alice could have sent are:
"aaadd", "abdd", "badd", "cdd", "aaae", "abe", "bae", and "ce".
Since there are 8 possible messages, we return 8.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> pressedKeys = "222222222222222222222222222222222222"
<strong>Output:</strong> 82876089
<strong>Explanation:</strong>
There are 2082876103 possible text messages Alice could have sent.
Since we need to return the answer modulo 10<sup>9</sup> + 7, we return 2082876103 % (10<sup>9</sup> + 7) = 82876089.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= pressedKeys.length &lt;= 10<sup>5</sup></code></li><li><code>pressedKeys</code>&nbsp;only consists of digits from&nbsp;<code>'2'</code>&nbsp;&#8211;&nbsp;<code>'9'</code>.</li></ul>



<h2><strong>Solution: DP</strong></h2>



<p>Similar to <a href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-91-decode-ways/" data-type="post" data-id="757">花花酱 LeetCode 91. Decode Ways</a>, let dp[i] denote # of possible messages of substr s[i:]</p>



<p>dp[i] = dp[i + 1] <br>+ dp[i + 2] (if s[i:i+1] are the same) <br>+ dp[i + 3] (if s[i:i+2] are the same) <br>+ dp[i + 4] (if s[i:i+3] are the same and s[i] in &#8217;79&#8217;)</p>



<p>dp[n] = 1</p>



<p>Time complexity: O(n)<br>Space complexity: O(n) -&gt; O(4)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def countTexts(self, s: str) -&gt; int:
    kMod = 10**9 + 7
    n = len(s)
    
    @cache
    def dp(i: int) -&gt; int:      
      if i &gt;= n: return 1
      ans = dp(i + 1)
      ans += dp(i + 2) if i + 2 &lt;= n and s[i] == s[i + 1] else 0
      ans += dp(i + 3) if i + 3 &lt;= n and s[i] == s[i + 1] == s[i + 2] else 0
      ans += dp(i + 4) if i + 4 &lt;= n and s[i] == s[i + 1] == s[i + 2] == s[i + 3] and s[i] in '79' else 0      
      return ans % kMod
    
    return dp(0)</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-2266-count-number-of-texts/">花花酱 LeetCode 2266. Count Number of Texts</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-2266-count-number-of-texts/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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-663c646404a99886649009/] 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>
