{"id":3226,"date":"2018-07-19T19:56:39","date_gmt":"2018-07-20T02:56:39","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3226"},"modified":"2018-07-20T00:29:29","modified_gmt":"2018-07-20T07:29:29","slug":"leetcode-115-distinct-subsequences","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-115-distinct-subsequences\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 115. Distinct Subsequences"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 115. Distinct Subsequences - \u5237\u9898\u627e\u5de5\u4f5c EP208\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/mPqqXh8XvWY?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<h1><strong>Problem<\/strong><\/h1>\n<p>Given a string\u00a0<strong>S<\/strong>\u00a0and a string\u00a0<strong>T<\/strong>, count the number of distinct subsequences of\u00a0<strong>S<\/strong>\u00a0which equals\u00a0<strong>T<\/strong>.<\/p>\n<p>A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie,\u00a0<code>\"ACE\"<\/code>\u00a0is a subsequence of\u00a0<code>\"ABCDE\"<\/code>\u00a0while\u00a0<code>\"AEC\"<\/code>\u00a0is not).<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input: <\/strong>S = \"rabbbit\", T = \"rabbit\"\r\n<strong>Output:<\/strong>\u00a03\r\n<strong>Explanation: <\/strong> As shown below, there are 3 ways you can generate \"rabbit\" from S. (The caret symbol ^ means the chosen letters) \r\nrabbbit\r\n^^^^ ^^\r\nrabbbit\r\n^^ ^^^^\r\nrabbbit\r\n^^^ ^^^<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>S = \"babgbag\", T = \"bag\"\r\n<strong>Output:<\/strong>\u00a05\r\n<strong>Explanation: <\/strong> As shown below, there are 5 ways you can generate \"bag\" from S. (The caret symbol ^ means the chosen letters)\r\nbabgbag\r\n^^ ^\r\nbabgbag\r\n^^ ^\r\nbabgbag\r\n^ ^^\r\nbabgbag\r\n^ ^^\r\nbabgbag\r\n^^^<\/pre>\n<h1><strong>Solution: DP<\/strong><\/h1>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3234\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/07\/115-ep208.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/07\/115-ep208.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/07\/115-ep208-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/07\/115-ep208-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p>Time complexity: O(|s| * |t|)<\/p>\n<p>Space complexity: O(|s| * |t|)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  int numDistinct(string s, string t) {\r\n    int ls = s.length();\r\n    int lt = t.length();\r\n    vector&lt;vector&lt;int&gt;&gt; dp(lt + 1, vector&lt;int&gt;(ls + 1));\r\n    fill(begin(dp[0]), end(dp[0]), 1);        \r\n    for (int i = 1; i &lt;= lt; ++i)\r\n      for (int j = 1; j &lt;= ls; ++j)\r\n        dp[i][j] = dp[i][j - 1] + (t[i - 1] == s[j - 1] ? dp[i - 1][j - 1] : 0);        \r\n    return dp[lt][ls];\r\n  }\r\n};<\/pre>\n<h1><strong>Related Problems:<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-72-edit-distance\/\">\u82b1\u82b1\u9171 LeetCode 72. Edit Distance<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a string\u00a0S\u00a0and a string\u00a0T, count the number of distinct subsequences of\u00a0S\u00a0which equals\u00a0T. A subsequence of a string is a new string which is&#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":[8,18,217,229],"class_list":["post-3226","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-counting","tag-dp","tag-hard","tag-subsequence","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3226","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=3226"}],"version-history":[{"count":8,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3226\/revisions"}],"predecessor-version":[{"id":3235,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3226\/revisions\/3235"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}