{"id":6657,"date":"2020-04-25T23:21:37","date_gmt":"2020-04-26T06:21:37","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6657"},"modified":"2020-04-25T23:21:38","modified_gmt":"2020-04-26T06:21:38","slug":"leetcode-1422-maximum-score-after-splitting-a-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1422-maximum-score-after-splitting-a-string\/","title":{"rendered":"LeetCode 1422. Maximum Score After Splitting a String"},"content":{"rendered":"\n<p>Given a&nbsp;string&nbsp;<code>s<\/code>&nbsp;of zeros and ones,&nbsp;<em>return the maximum score after splitting the string into two&nbsp;<strong>non-empty<\/strong>&nbsp;substrings<\/em>&nbsp;(i.e.&nbsp;<strong>left<\/strong>&nbsp;substring and&nbsp;<strong>right<\/strong>&nbsp;substring).<\/p>\n\n\n\n<p>The score after splitting a string is the number of&nbsp;<strong>zeros<\/strong>&nbsp;in the&nbsp;<strong>left<\/strong>&nbsp;substring plus the number of&nbsp;<strong>ones<\/strong>&nbsp;in the&nbsp;<strong>right<\/strong>&nbsp;substring.<\/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 = \"011101\"\n<strong>Output:<\/strong> 5 \n<strong>Explanation:<\/strong> \nAll possible ways of splitting s into two non-empty substrings are:\nleft = \"0\" and right = \"11101\", score = 1 + 4 = 5 \nleft = \"01\" and right = \"1101\", score = 1 + 3 = 4 \nleft = \"011\" and right = \"101\", score = 1 + 2 = 3 \nleft = \"0111\" and right = \"01\", score = 1 + 1 = 2 \nleft = \"01110\" and right = \"1\", score = 2 + 1 = 3\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 = \"00111\"\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> When left = \"00\" and right = \"111\", we get the maximum score = 2 + 3 = 5\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 = \"1111\"\n<strong>Output:<\/strong> 3\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= s.length &lt;= 500<\/code><\/li><li>The string&nbsp;<code>s<\/code>&nbsp;consists of characters &#8216;0&#8217; and &#8216;1&#8217; only.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n^2)<br>Space complexity: O(1)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Counting<\/strong><\/h2>\n\n\n\n<p>2.1 Two passes,<br>1st, count the number of ones of the entire string<br>2nd, inc zeros or dec ones according to s[i]<br>ans = max(zeros + ones)<\/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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maxScore(string s) {\n    int ones = 0;\n    for (char c : s) \n      if (c == '1') ++ones;\n    int zeros = 0;\n    int ans = 0;\n    for (int i = 0; i < s.length() - 1; ++i) {\n      if (s[i] == '0') ++zeros;\n      else --ones;\n      ans = max(ans, zeros + ones);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;string&nbsp;s&nbsp;of zeros and ones,&nbsp;return the maximum score after splitting the string into two&nbsp;non-empty&nbsp;substrings&nbsp;(i.e.&nbsp;left&nbsp;substring and&nbsp;right&nbsp;substring). The score after splitting a string is the number of&nbsp;zeros&nbsp;in&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[20,222,179],"class_list":["post-6657","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-array","tag-easy","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6657","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=6657"}],"version-history":[{"count":1,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6657\/revisions"}],"predecessor-version":[{"id":6658,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6657\/revisions\/6658"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}