{"id":9731,"date":"2022-05-10T07:47:44","date_gmt":"2022-05-10T14:47:44","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9731"},"modified":"2022-05-10T07:48:29","modified_gmt":"2022-05-10T14:48:29","slug":"leetcode-2266-count-number-of-texts","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-2266-count-number-of-texts\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2266. Count Number of Texts"},"content":{"rendered":"\n<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>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/03\/15\/1200px-telephone-keypad2svg.png\" alt=\"\"\/><\/figure>\n\n\n\n<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>\n\n\n\n<ul class=\"wp-block-list\"><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>\n\n\n\n<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>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, when Alice sent the message&nbsp;<code>\"bob\"<\/code>, Bob received the string&nbsp;<code>\"2266622\"<\/code>.<\/li><\/ul>\n\n\n\n<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>\n\n\n\n<p>Since the answer may be very large, return it&nbsp;<strong>modulo<\/strong>&nbsp;<code>10<sup>9<\/sup>&nbsp;+ 7<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> pressedKeys = \"22233\"\n<strong>Output:<\/strong> 8\n<strong>Explanation:<\/strong>\nThe possible text messages Alice could have sent are:\n\"aaadd\", \"abdd\", \"badd\", \"cdd\", \"aaae\", \"abe\", \"bae\", and \"ce\".\nSince there are 8 possible messages, we return 8.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> pressedKeys = \"222222222222222222222222222222222222\"\n<strong>Output:<\/strong> 82876089\n<strong>Explanation:<\/strong>\nThere are 2082876103 possible text messages Alice could have sent.\nSince we need to return the answer modulo 10<sup>9<\/sup> + 7, we return 2082876103 % (10<sup>9<\/sup> + 7) = 82876089.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><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>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>Similar to <a href=\"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-91-decode-ways\/\" data-type=\"post\" data-id=\"757\">\u82b1\u82b1\u9171 LeetCode 91. Decode Ways<\/a>, let dp[i] denote # of possible messages of substr s[i:]<\/p>\n\n\n\n<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>\n\n\n\n<p>dp[n] = 1<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n) -&gt; O(4)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def countTexts(self, s: str) -> int:\n    kMod = 10**9 + 7\n    n = len(s)\n    \n    @cache\n    def dp(i: int) -> int:      \n      if i >= n: return 1\n      ans = dp(i + 1)\n      ans += dp(i + 2) if i + 2 <= n and s[i] == s[i + 1] else 0\n      ans += dp(i + 3) if i + 3 <= n and s[i] == s[i + 1] == s[i + 2] else 0\n      ans += dp(i + 4) if i + 4 <= n and s[i] == s[i + 1] == s[i + 2] == s[i + 3] and s[i] in '79' else 0      \n      return ans % kMod\n    \n    return dp(0)\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Alice is texting Bob using her phone. The&nbsp;mapping&nbsp;of digits to letters is shown in the figure below. In order to&nbsp;add&nbsp;a letter, Alice has to&nbsp;press&nbsp;the key&#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],"tags":[155,18,177],"class_list":["post-9731","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-decode","tag-dp","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9731","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=9731"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9731\/revisions"}],"predecessor-version":[{"id":9733,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9731\/revisions\/9733"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}