{"id":7330,"date":"2020-09-05T11:29:04","date_gmt":"2020-09-05T18:29:04","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7330"},"modified":"2020-09-05T17:25:50","modified_gmt":"2020-09-06T00:25:50","slug":"leetcode-1573-number-of-ways-to-split-a-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1573-number-of-ways-to-split-a-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1573. Number of Ways to Split a String"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1573. Number of Ways to Split a String - \u5237\u9898\u627e\u5de5\u4f5c EP354\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/gkFKRbkrIws?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>\n<\/div><\/figure>\n\n\n\n<p>Given a binary string&nbsp;<code>s<\/code>&nbsp;(a string consisting only of &#8216;0&#8217;s and &#8216;1&#8217;s),&nbsp;we can split&nbsp;<code>s<\/code>&nbsp;into 3&nbsp;<strong>non-empty<\/strong>&nbsp;strings s1, s2, s3 (s1+ s2+ s3 = s).<\/p>\n\n\n\n<p>Return the number of ways&nbsp;<code>s<\/code>&nbsp;can be split such that the number of&nbsp;characters &#8216;1&#8217; is the same in s1, s2, and s3.<\/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 = \"10101\"\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> There are four ways to split s in 3 parts where each part contain the same number of letters '1'.\n\"1|010|1\"\n\"1|01|01\"\n\"10|10|1\"\n\"10|1|01\"\n<\/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 = \"1001\"\n<strong>Output:<\/strong> 0\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 = \"0000\"\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> There are three ways to split s in 3 parts.\n\"0|0|00\"\n\"0|00|0\"\n\"00|0|0\"\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 = \"100100010100110\"\n<strong>Output:<\/strong> 12\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>3 &lt;= s.length &lt;= 10^5<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Counting<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/09\/1573-ep354-1.png\" alt=\"\" class=\"wp-image-7334\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/09\/1573-ep354-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/09\/1573-ep354-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/09\/1573-ep354-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/09\/1573-ep354-2.png\" alt=\"\" class=\"wp-image-7335\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/09\/1573-ep354-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/09\/1573-ep354-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/09\/1573-ep354-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Count how many ones in the binary string as T, if not a factor of 3, then there is no answer.<\/p>\n\n\n\n<p>Count how many positions that have prefix sum of T\/3 as l, and how many positions that have prefix sum of T\/3*2 as r.<\/p>\n\n\n\n<p>Ans = l * r<\/p>\n\n\n\n<p>But we need to special handle the all zero cases, which equals to C(n-2, 2) = (n &#8211; 1) * (n &#8211; 2) \/ 2<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: 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++\">\nclass Solution {\npublic:\n  int numWays(string s) {\n    const int kMod = 1e9 + 7;\n    const long n = s.length();\n    int t = 0;\n    for (char c : s) t += c == '1';\n    if (t % 3) return 0;\n    if (t == 0)\n      return ((1 + (n - 2)) * (n - 2) \/ 2) % kMod;\n    t \/= 3;\n    long l = 0;\n    long r = 0;\n    for (int i = 0, c = 0; i < n; ++i) {\n      c += (s[i] == '1');\n      if (c == t) ++l;\n      else if (c == t * 2) ++r;\n    }\n    return (l * r) % kMod;\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 numWays(self, s: str) -> int:\n    n = len(s)\n    t = s.count('1')\n    if t % 3 != 0: return 0\n    if t == 0: return ((n - 1) * (n - 2) \/\/ 2) % (10**9 + 7)\n    t \/\/= 3\n    l, r, c = 0, 0, 0\n    for i, ch in enumerate(s):\n      if ch == '1': c += 1\n      if c == t: l += 1\n      elif c == t * 2: r += 1\n    return (l * r) % (10**9 + 7)        \n<\/pre>\n<\/div><\/div>\n\n\n\n<p>One pass: Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\nclass Solution:\n  def numWays(self, s: str) -> int:\n    n = len(s)\n    p = defaultdict(int)\n    c = 0\n    for ch in s:\n      if ch == '1': c += 1\n      p[c] += 1\n    if c % 3 != 0: return 0\n    if c == 0: return ((n - 1) * (n - 2) \/\/ 2) % (10**9 + 7)\n    return (p[c \/\/ 3] * p[c \/\/ 3 * 2]) % (10**9 + 7)\n<\/pre>\n<\/div><\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>Given a binary string&nbsp;s&nbsp;(a string consisting only of &#8216;0&#8217;s and &#8216;1&#8217;s),&nbsp;we can split&nbsp;s&nbsp;into 3&nbsp;non-empty&nbsp;strings s1, s2, s3 (s1+ s2+ s3 = s). Return the number&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[122,31],"class_list":["post-7330","post","type-post","status-publish","format-standard","hentry","category-math","tag-combination","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7330","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=7330"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7330\/revisions"}],"predecessor-version":[{"id":7337,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7330\/revisions\/7337"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}