{"id":5753,"date":"2019-10-13T19:34:26","date_gmt":"2019-10-14T02:34:26","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5753"},"modified":"2019-10-13T19:35:00","modified_gmt":"2019-10-14T02:35:00","slug":"leetcode-1221-split-a-string-in-balanced-strings","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1221-split-a-string-in-balanced-strings\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1221. Split a String in Balanced Strings"},"content":{"rendered":"\n<p><em>Balanced<\/em>&nbsp;strings are those who have equal quantity of &#8216;L&#8217; and &#8216;R&#8217; characters.<\/p>\n\n\n\n<p>Given a balanced string&nbsp;<code>s<\/code>&nbsp;split it in the maximum amount of balanced strings.<\/p>\n\n\n\n<p>Return the maximum amount of splitted balanced strings.<\/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 = \"RLRRLLRLRL\"\n<strong>Output:<\/strong> 4\n<strong>Explanation: <\/strong>s can be split into \"RL\", \"RRLL\", \"RL\", \"RL\", each substring contains same number of 'L' and 'R'.\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 = \"RLLLLRRRLR\"\n<strong>Output:<\/strong> 3\n<strong>Explanation: <\/strong>s can be split into \"RL\", \"LLLRRR\", \"LR\", each substring contains same number of 'L' and 'R'.\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 = \"LLLLRRRR\"\n<strong>Output:<\/strong> 1\n<strong>Explanation: <\/strong>s can be split into \"LLLLRRRR\".\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 1000<\/code><\/li><li><code>s[i] = 'L' or 'R'<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution<\/strong>: <strong>Greedy<\/strong><\/h2>\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++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  int balancedStringSplit(string s) {\n    int b = 0;\n    int ans = 0;\n    for (char c : s) {\n      b += c == 'L' ? 1 : -1;\n      if (b == 0) ++ans;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Balanced&nbsp;strings are those who have equal quantity of &#8216;L&#8217; and &#8216;R&#8217; characters. Given a balanced string&nbsp;s&nbsp;split it in the maximum amount of balanced strings. Return&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[509,222,88,4],"class_list":["post-5753","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-balanced","tag-easy","tag-greedy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5753","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=5753"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5753\/revisions"}],"predecessor-version":[{"id":5756,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5753\/revisions\/5756"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}