{"id":74594,"date":"2026-02-10T07:58:10","date_gmt":"2026-02-09T23:58:10","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/74594.html"},"modified":"2026-02-10T07:58:10","modified_gmt":"2026-02-09T23:58:10","slug":"leetcode-%e5%9b%b0%e9%9a%be%e9%a2%98-940-distinct-subsequences-ii-%e4%b8%8d%e5%90%8c%e7%9a%84%e5%ad%90%e5%ba%8f%e5%88%97-ii-%e8%80%97%e6%97%b6100","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/74594.html","title":{"rendered":"leetcode \u56f0\u96be\u9898 940. Distinct Subsequences II \u4e0d\u540c\u7684\u5b50\u5e8f\u5217 II-\u8017\u65f6100"},"content":{"rendered":"<p>Problem: 940. Distinct Subsequences II \u4e0d\u540c\u7684\u5b50\u5e8f\u5217 II<\/p>\n<p>\u8017\u65f6100%&#xff0c;\u52a8\u6001\u89c4\u5212\u7684&#xff0c;\u8003\u8651\u4e24\u79cd\u60c5\u51b5&#xff0c;\u6bcf\u79cd\u5b57\u7b26\u90fd\u4e0d\u76f8\u540c&#xff0c;\u5b58\u5728\u76f8\u540c\u7684\u5b57\u7b26&#xff0c;dp[i]\u8868\u793as[0&#xff5e;i]\u5b50\u5b57\u7b26\u4e32\u7684\u603b\u6570&#xff0c;\u82e5\u662fs[0~i-1]\u4e0d\u5b58\u5728s[i]&#xff0c;\u90a3\u4e48\u7ed3\u679c\u5c31\u662fdp[i] &#061; (dp[i-1] * 2) &#043; 1;\u4e5f\u5c31\u662f\u4e4b\u524d\u7684\u6240\u6709\u5b50\u5b57\u7b26\u4e32\u6700\u540e\u9762\u52a0\u4e0as[i]\u5373\u53ef&#xff0c;\u4ee5\u53ca\u5305\u542b\u4e86s[i]\u8fd9\u4e2a\u5355\u4e2a\u5b57\u7b26<\/p>\n<p>\u82e5s[0~i-1]\u5b58\u5728s[i]&#xff0c;\u5047\u8bbes[j]s[i] (j &lt; i)&#xff0c;\u90a3\u4e48s[0<sub>i-1]\u7684\u5b50\u5b57\u7b26\u4e32s[j]\u53ef\u4ee5\u88abs[i]\u66ff\u4ee3&#xff0c;\u76f8\u5f53\u4e8es[0<\/sub>j-1]\u7684\u5b50\u5b57\u7b26\u4e32\u6dfb\u52a0\u4e0as[i]\u6216s[j]&#xff0c;\u6b64\u65f6\u672b\u5c3e\u52a0\u4e0as[i]\u6216\u8005s[j]\u6548\u679c\u76f8\u540c&#xff0c;\u6240\u4ee5\u91cd\u590d\u4e86\u4e00\u6b21&#xff0c;\u9700\u8981\u51cf\u53bb\u7684\u4e5f\u5c31\u662fdp[i] &#061; dp[i] &#8211; 1 &#8211; dp[j-1] &#043; modulo; -1\u662f\u5355\u5b57\u7b26s[i]\u672c\u8eab\u91cd\u590d\u4e86\u4e00\u6b21&#xff0c;\u800c\u4e14\u5f53j0\u65f6&#xff0c;\u9700\u8981\u7279\u6b8a\u8003\u8651\u6b64\u65f6dp[i] &#061; dp[i] &#8211; 1 &#043; modulo<\/p>\n<p>\u8003\u8651\u5230dp[i] &#061; dp[i] &#8211; 1 &#8211; dp[ind-1]\u53ef\u80fd&lt;0&#xff0c;\u6240\u4ee5\u9700\u8981\u52a0\u4e0a\u6a211e9&#043;7<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260209235806-698a748e58518.png\" alt=\"image.png\" \/><\/p>\n<h2>Code<\/h2>\n<p>class Solution {<br \/>\npublic:<br \/>\n    int ch[26];<br \/>\n    const long long modulo &#061; 1e9 &#043; 7;<br \/>\n    int distinctSubseqII(string s) {<br \/>\n        fill(ch, ch &#043; 26, -1);<br \/>\n        int n &#061; s.size(), ind;<br \/>\n        if(n&#061;&#061;1) return 1;<br \/>\n        vector&lt;long long&gt; dp(n, 0);<br \/>\n        dp[0] &#061; 1;<br \/>\n        ch[s[0]-&#039;a&#039;] &#061; 0;<br \/>\n        for(int i &#061; 1; i &lt; n; i&#043;&#043;) {<br \/>\n            dp[i] &#061; (dp[i-1] &lt;&lt; 1) &#043; 1;<br \/>\n            ind &#061; ch[s[i]-&#039;a&#039;];<br \/>\n            if(ind &#061;&#061; 0) {<br \/>\n                dp[i] &#061; dp[i] &#8211; 1 &#043; modulo;<br \/>\n            } else if(ind &gt; 0){<br \/>\n                dp[i] &#061; dp[i] &#8211; 1 &#8211; dp[ind-1] &#043; modulo;<br \/>\n            }<br \/>\n            ch[s[i]-&#039;a&#039;] &#061; i;<br \/>\n            dp[i] &#061; dp[i] % modulo;<br \/>\n        }<br \/>\n        int ret &#061; dp[n-1] % modulo;<br \/>\n        return ret;<br \/>\n    }<br \/>\n};<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: 940. Distinct Subsequences II \u4e0d\u540c\u7684\u5b50\u5e8f\u5217 II \u8017\u65f6100%&#xff0c;\u52a8\u6001\u89c4\u5212\u7684&#xff0c;\u8003\u8651\u4e24\u79cd\u60c5\u51b5&#xff0c;\u6bcf\u79cd\u5b57\u7b26\u90fd\u4e0d\u76f8\u540c&#xff0c;\u5b58\u5728\u76f8\u540c\u7684\u5b57\u7b26&#xff0c;dp[i]\u8868\u793as[0&#xff5e;i]\u5b50\u5b57\u7b26\u4e32\u7684\u603b\u6570&#xff0c;\u82e5\u662fs[0~i-1]\u4e0d\u5b58\u5728s[i]&#xff0c;\u90a3\u4e48\u7ed3\u679c\u5c31\u662fdp[i]  (dp[i-1] * 2)  1;\u4e5f\u5c31\u662f\u4e4b\u524d\u7684\u6240\u6709\u5b50\u5b57\u7b26\u4e32\u6700\u540e\u9762\u52a0\u4e0as[i]\u5373\u53ef&amp;#xff0<\/p>\n","protected":false},"author":2,"featured_media":74593,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2692,427,925],"topic":[],"class_list":["post-74594","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-leetcode","tag-427","tag-925"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>leetcode \u56f0\u96be\u9898 940. Distinct Subsequences II \u4e0d\u540c\u7684\u5b50\u5e8f\u5217 II-\u8017\u65f6100 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/74594.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"leetcode \u56f0\u96be\u9898 940. Distinct Subsequences II \u4e0d\u540c\u7684\u5b50\u5e8f\u5217 II-\u8017\u65f6100 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"Problem: 940. Distinct Subsequences II \u4e0d\u540c\u7684\u5b50\u5e8f\u5217 II \u8017\u65f6100%&#xff0c;\u52a8\u6001\u89c4\u5212\u7684&#xff0c;\u8003\u8651\u4e24\u79cd\u60c5\u51b5&#xff0c;\u6bcf\u79cd\u5b57\u7b26\u90fd\u4e0d\u76f8\u540c&#xff0c;\u5b58\u5728\u76f8\u540c\u7684\u5b57\u7b26&#xff0c;dp[i]\u8868\u793as[0&#xff5e;i]\u5b50\u5b57\u7b26\u4e32\u7684\u603b\u6570&#xff0c;\u82e5\u662fs[0~i-1]\u4e0d\u5b58\u5728s[i]&#xff0c;\u90a3\u4e48\u7ed3\u679c\u5c31\u662fdp[i] (dp[i-1] * 2) 1;\u4e5f\u5c31\u662f\u4e4b\u524d\u7684\u6240\u6709\u5b50\u5b57\u7b26\u4e32\u6700\u540e\u9762\u52a0\u4e0as[i]\u5373\u53ef&amp;#xff0\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/74594.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-09T23:58:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260209235806-698a748e58518.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/74594.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/74594.html\",\"name\":\"leetcode \u56f0\u96be\u9898 940. Distinct Subsequences II \u4e0d\u540c\u7684\u5b50\u5e8f\u5217 II-\u8017\u65f6100 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-09T23:58:10+00:00\",\"dateModified\":\"2026-02-09T23:58:10+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/74594.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/74594.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/74594.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"leetcode \u56f0\u96be\u9898 940. Distinct Subsequences II \u4e0d\u540c\u7684\u5b50\u5e8f\u5217 II-\u8017\u65f6100\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"leetcode \u56f0\u96be\u9898 940. Distinct Subsequences II \u4e0d\u540c\u7684\u5b50\u5e8f\u5217 II-\u8017\u65f6100 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/74594.html","og_locale":"zh_CN","og_type":"article","og_title":"leetcode \u56f0\u96be\u9898 940. Distinct Subsequences II \u4e0d\u540c\u7684\u5b50\u5e8f\u5217 II-\u8017\u65f6100 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"Problem: 940. Distinct Subsequences II \u4e0d\u540c\u7684\u5b50\u5e8f\u5217 II \u8017\u65f6100%&#xff0c;\u52a8\u6001\u89c4\u5212\u7684&#xff0c;\u8003\u8651\u4e24\u79cd\u60c5\u51b5&#xff0c;\u6bcf\u79cd\u5b57\u7b26\u90fd\u4e0d\u76f8\u540c&#xff0c;\u5b58\u5728\u76f8\u540c\u7684\u5b57\u7b26&#xff0c;dp[i]\u8868\u793as[0&#xff5e;i]\u5b50\u5b57\u7b26\u4e32\u7684\u603b\u6570&#xff0c;\u82e5\u662fs[0~i-1]\u4e0d\u5b58\u5728s[i]&#xff0c;\u90a3\u4e48\u7ed3\u679c\u5c31\u662fdp[i] (dp[i-1] * 2) 1;\u4e5f\u5c31\u662f\u4e4b\u524d\u7684\u6240\u6709\u5b50\u5b57\u7b26\u4e32\u6700\u540e\u9762\u52a0\u4e0as[i]\u5373\u53ef&amp;#xff0","og_url":"https:\/\/www.wsisp.com\/helps\/74594.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-09T23:58:10+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260209235806-698a748e58518.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/74594.html","url":"https:\/\/www.wsisp.com\/helps\/74594.html","name":"leetcode \u56f0\u96be\u9898 940. Distinct Subsequences II \u4e0d\u540c\u7684\u5b50\u5e8f\u5217 II-\u8017\u65f6100 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-09T23:58:10+00:00","dateModified":"2026-02-09T23:58:10+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/74594.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/74594.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/74594.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"leetcode \u56f0\u96be\u9898 940. Distinct Subsequences II \u4e0d\u540c\u7684\u5b50\u5e8f\u5217 II-\u8017\u65f6100"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/74594","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=74594"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/74594\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/74593"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=74594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=74594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=74594"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=74594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}