{"id":4309,"date":"2018-11-14T01:04:29","date_gmt":"2018-11-14T09:04:29","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4309"},"modified":"2018-11-14T01:06:28","modified_gmt":"2018-11-14T09:06:28","slug":"leetcode-940-distinct-subsequences-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-940-distinct-subsequences-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 940. Distinct Subsequences II"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a string\u00a0<code>S<\/code>, count the number of distinct, non-empty subsequences of\u00a0<code>S<\/code>\u00a0.<\/p>\n<p>Since the result may be large,\u00a0<strong>return the answer modulo\u00a0<code>10^9 + 7<\/code><\/strong>.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"&quot;crayon:false\u201d\"><strong>Input: <\/strong><span id=\"example-input-1-1\">\"abc\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">7<\/span>\r\n<strong>Explanation<\/strong>: The 7 distinct subsequences are \"a\", \"b\", \"c\", \"ab\", \"ac\", \"bc\", and \"abc\".\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"&quot;crayon:false\u201d\"><strong>Input: <\/strong><span id=\"example-input-2-1\">\"aba\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">6\r\n<\/span><strong>Explanation<\/strong>: The 6 distinct subsequences are \"a\", \"b\", \"ab\", \"ba\", \"aa\" and \"aba\".\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"&quot;crayon:false\u201d\"><strong>Input: <\/strong><span id=\"example-input-3-1\">\"aaa\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">3\r\n<\/span><strong>Explanation<\/strong>: The 3 distinct subsequences are \"a\", \"aa\" and \"aaa\".<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>S<\/code>\u00a0contains only lowercase letters.<\/li>\n<li><code>1 &lt;= S.length &lt;= 2000<\/code><\/li>\n<\/ol>\n<h1><strong>Solution: DP<\/strong><\/h1>\n<p class=\"\">counts[i][j] := # of distinct sub sequences of s[1-&gt;i] and ends with letter j. (&#8216;a'&lt;= j &lt;= &#8216;z&#8217;)<\/p>\n<p class=\"\">Initialization:<\/p>\n<p class=\"\">counts[*][*] = 0<\/p>\n<p class=\"\">Transition:<\/p>\n<p class=\"\">counts[i][j] = sum(counts[i-1]) + 1 if s[i] == j\u00a0 else counts[i-1][j]<\/p>\n<p class=\"\">ans = sum(counts[n])<\/p>\n<p>e.g. S = &#8220;abc&#8221;<\/p>\n<p>counts[1] = {&#8216;a&#8217; : 1}<br \/>\ncounts[2] = {&#8216;a&#8217; : 1, &#8216;b&#8217; : 1 + 1 = 2}<br \/>\ncounts[3] = {&#8216;a&#8217; : 1, &#8216;b&#8217; : 2, &#8216;c&#8217;: 1 + 2 + 1 = 4}<br \/>\nans = sum(counts[3]) = 1 + 2 + 4 = 7<\/p>\n<p>Time complexity: O(N*26)<\/p>\n<p>Space complexity: O(N*26) -&gt; O(26)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua, running time: 12 ms\r\nclass Solution {\r\npublic:\r\n  int distinctSubseqII(string S) {\r\n    constexpr int kMod = 1e9 + 7;\r\n    std::vector&lt;int&gt; counts(26);\r\n    for (char c : S)\r\n      counts[c - 'a'] = accumulate(begin(counts), end(counts), 1L) % kMod;\r\n    return accumulate(begin(counts), end(counts), 0L) % kMod;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true \"># Author: Huahua, running time: 64 ms\r\nclass Solution:\r\n  def distinctSubseqII(self, S):\r\n    kMod = 10**9 + 7\r\n    dp = {}\r\n    for c in S:\r\n      dp[c] = (sum(dp.values()) + 1) % kMod\r\n    return sum(dp.values()) % kMod<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a string\u00a0S, count the number of distinct, non-empty subsequences of\u00a0S\u00a0. Since the result may be large,\u00a0return the answer modulo\u00a010^9 + 7. Example 1:&#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":[18,217,229],"class_list":["post-4309","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-hard","tag-subsequence","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4309","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=4309"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4309\/revisions"}],"predecessor-version":[{"id":4312,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4309\/revisions\/4312"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}