{"id":7180,"date":"2020-07-29T12:12:56","date_gmt":"2020-07-29T19:12:56","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7180"},"modified":"2020-07-29T12:19:53","modified_gmt":"2020-07-29T19:19:53","slug":"leetcode-1513-number-of-substrings-with-only-1s","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1513-number-of-substrings-with-only-1s\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1513. Number of Substrings With Only 1s"},"content":{"rendered":"\n<p>Given a binary string&nbsp;<code>s<\/code>&nbsp;(a string consisting only of &#8216;0&#8217; and &#8216;1&#8217;s).<\/p>\n\n\n\n<p>Return the number of substrings with all characters 1&#8217;s.<\/p>\n\n\n\n<p>Since the answer&nbsp;may be too large,&nbsp;return it modulo&nbsp;10^9 + 7.<\/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> s = \"0110111\"\n<strong>Output:<\/strong> 9\n<strong>Explanation: <\/strong>There are 9 substring in total with only 1's characters.\n\"1\" -&gt; 5 times.\n\"11\" -&gt; 3 times.\n\"111\" -&gt; 1 time.<\/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> s = \"101\"\n<strong>Output:<\/strong> 2\n<strong>Explanation: <\/strong>Substring \"1\" is shown 2 times in s.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"111111\"\n<strong>Output:<\/strong> 21\n<strong>Explanation: <\/strong>Each substring contains only 1's characters.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"000\"\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>s[i] == '0'<\/code>&nbsp;or&nbsp;<code>s[i] == '1'<\/code><\/li><li><code>1 &lt;= s.length &lt;= 10^5<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong> <strong>\/ Prefix Sum<\/strong><\/h2>\n\n\n\n<p>dp[i] := # of all 1 subarrays end with s[i].<br>dp[i] = dp[i-1] if s[i] == \u20181\u2018 else 0<br>ans = sum(dp)<br>s=1101<br>dp[0] = 1 \/\/ 1<br>dp[1] = 2 \/\/ 11, *1<br>dp[2] = 0 \/\/ None<br>dp[3] = 1 \/\/ ***1<br>ans = 1 + 2 + 1 = 5<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int numSub(string s) {\n    constexpr int kMod = 1e9 + 7;\n    long ans = 0;\n    vector<int> dp(s.length() + 1);\n    for (int i = 1; i <= s.length(); ++i) {\n      dp[i] = s[i - 1] == '1' ? dp[i - 1] + 1 : 0;\n      ans += dp[i];\n    }\n    return ans % kMod;\n  }\n};\n<\/pre> \n<\/div><\/div>\n\n\n\n<p>dp[i] only depends on dp[i-1], we can reduce the space complexity to O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">class Solution {\npublic:\n  int numSub(string s) {\n    constexpr int kMod = 1e9 + 7;\n    long ans = 0;\n    int cur = 0;\n    for (char c : s) {\n      cur = c == '1' ? cur + 1 : 0;\n      ans += cur;\n    }\n    return ans % kMod;\n  }\n};\n<\/pre> \n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"java\">\nclass Solution {\n  public int numSub(String s) {\n    final int kMod = 1_000_000_000 + 7;\n    int ans = 0;\n    int cur = 0;\n    for (int i = 0; i < s.length(); ++i) {\n      cur = s.charAt(i) == '1' ? cur + 1 : 0;\n      ans = (ans + cur) % kMod;\n    }\n    return ans;\n  }\n}\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\nclass Solution:\n  def numSub(self, s: str) -> int:\n    kMod = 10**9 + 7\n    ans = 0\n    cur = 0\n    for c in s:\n      cur = cur + 1 if c == '1' else 0\n      ans += cur\n    return ans % kMod\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary string&nbsp;s&nbsp;(a string consisting only of &#8216;0&#8217; and &#8216;1&#8217;s). Return the number of substrings with all characters 1&#8217;s. Since the answer&nbsp;may be too&#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,177,200,41],"class_list":["post-7180","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-medium","tag-prefix-sum","tag-subarray","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7180","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=7180"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7180\/revisions"}],"predecessor-version":[{"id":7187,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7180\/revisions\/7187"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}