{"id":49,"date":"2018-11-12T16:14:18","date_gmt":"2018-11-12T16:14:18","guid":{"rendered":"https:\/\/andybase.com\/?p=49"},"modified":"2018-11-27T14:43:43","modified_gmt":"2018-11-27T14:43:43","slug":"940-distinct-subsequences-ii","status":"publish","type":"post","link":"https:\/\/andybase.com\/?p=49","title":{"rendered":"940 Distinct Subsequences II"},"content":{"rendered":"\n<p>link <a href=\"https:\/\/leetcode.com\/problems\/distinct-subsequences-ii\/\">Distinct Subsequences II<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Thought<\/h2>\n\n\n\n<p>The very first thought here is use DFS to find all distinct subsequences. However it will time out. So my next idea is using dynamic programming to solve it. I&#8217;ll define a DP array, where DP[i + 1] stands for how many distinct subsequences if I use 0,1,&#8230;i characters to build sub sequences, <strong>end with character i<\/strong>. DP[0] is special because it stands for empty condition and should always be 1.<\/p>\n\n\n\n<p>Then, the state transfer funtion is:<\/p>\n\n\n\n<p>DP[i] = sum(DP[i-1], DP[i-2], &#8230; DP[0])<\/p>\n\n\n\n<p>Next, lets think about dumplicate. Assume the case &#8220;abaca&#8221;. When I see the last &#8216;a&#8217;, what should I put in DP[5]? I should only count DP[3],DP[4] cause DP[2] + &#8216;a&#8217; is already computed in DP[3]. So I put another dictionary, with character as key, its <strong>last<\/strong> index so far as value. Then the state transfer function change a little bit to:<\/p>\n\n\n\n<p>DP[i] = sum(DP[i-1], DP[i-2], &#8230; DP[last_time_same_character_appears])<\/p>\n\n\n\n<p>And remember DP[0] stands for empty result, when we count final results, we should emit it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code<\/h2>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code brush: python; notranslate\">class Solution:\n    def distinctSubseqII(self, S):\n        \"\"\"\n        :type S: str\n        :rtype: int\n        \"\"\"\n        MOD = 10**9 + 7\n        N = len(S)\n        DP = [0] * (N + 1)\n        DP[0] = 1\n        lookup = collections.defaultdict(int)\n        for index, val in enumerate(S):\n            q = index\n            prev = lookup[val]\n            DP[index + 1] = sum(DP[prev:])  % MOD\n            lookup[val] = index + 1\n        return sum(DP[1:]) % MOD\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Runtime<\/h2>\n\n\n\n<p>Time: O(n^2)<br>Space: O(n)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>link Distinct Subsequences II Thought The very first thought here is use DFS to find all distinct subsequences. However it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[2],"tags":[4,3],"class_list":["post-49","post","type-post","status-publish","format-standard","hentry","category-leetcode","tag-contest","tag-leetcode"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paWPni-N","_links":{"self":[{"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/posts\/49","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/andybase.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=49"}],"version-history":[{"count":2,"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/posts\/49\/revisions"}],"predecessor-version":[{"id":82,"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/posts\/49\/revisions\/82"}],"wp:attachment":[{"href":"https:\/\/andybase.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/andybase.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/andybase.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}