{"id":7143,"date":"2020-07-25T13:58:50","date_gmt":"2020-07-25T20:58:50","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7143"},"modified":"2020-07-25T14:00:22","modified_gmt":"2020-07-25T21:00:22","slug":"leetcode-1525-number-of-good-ways-to-split-a-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/sliding-window\/leetcode-1525-number-of-good-ways-to-split-a-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1525. Number of Good Ways to Split a String"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>s<\/code>, a&nbsp;split is called&nbsp;<em>good<\/em>&nbsp;if you can split&nbsp;<code>s<\/code>&nbsp;into 2&nbsp;non-empty strings&nbsp;<code>p<\/code>&nbsp;and&nbsp;<code>q<\/code>&nbsp;where its concatenation is equal to&nbsp;<code>s<\/code>&nbsp;and the number of distinct letters in&nbsp;<code>p<\/code>&nbsp;and&nbsp;<code>q<\/code>&nbsp;are the same.<\/p>\n\n\n\n<p>Return the number of&nbsp;<em>good<\/em>&nbsp;splits you can make in&nbsp;<code>s<\/code>.<\/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 = \"aacaba\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> There are 5 ways to split <code>\"aacaba\"<\/code> and 2 of them are good. \n(\"a\", \"acaba\") Left string and right string contains 1 and 3 different letters respectively.\n(\"aa\", \"caba\") Left string and right string contains 1 and 3 different letters respectively.\n(\"aac\", \"aba\") Left string and right string contains 2 and 2 different letters respectively (good split).\n(\"aaca\", \"ba\") Left string and right string contains 2 and 2 different letters respectively (good split).\n(\"aacab\", \"a\") Left string and right string contains 3 and 1 different letters respectively.\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 = \"abcd\"\n<strong>Output:<\/strong> 1\n<strong>Explanation: <\/strong>Split the string as follows (\"ab\", \"cd\").\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 = \"aaaaa\"\n<strong>Output:<\/strong> 4\n<strong>Explanation: <\/strong>All possible splits are good.<\/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 = \"acbadbaada\"\n<strong>Output:<\/strong> 2\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>s<\/code>&nbsp;contains only lowercase English letters.<\/li><li><code>1 &lt;= s.length &lt;= 10^5<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sliding Window<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>Count the frequency of each letter and count number of unique letters for the entire string as right part.<\/li><li>Iterate over the string, add current letter to the left part, and remove it from the right part.<\/li><li>We only<ol><li>increase the number of unique letters when its frequency becomes to 1<\/li><li>decrease the number of unique letters when its frequency becomes to 0  <\/li><\/ol><\/li><\/ol>\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 numSplits(string s) {\n    vector<int> r(26);\n    vector<int> l(26);\n    int cr = 0;\n    for (char c : s)\n      cr += (++r[c - 'a'] == 1);    \n    int ans = 0;\n    int cl = 0;\n    for (char c : s) {\n      cl += (++l[c - 'a'] == 1);\n      cr -= (--r[c - 'a'] == 0);\n      ans += cl == cr;\n    }\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">java<\/h2>\n<div class=\"tabcontent\">\n\n<pre>\nclass Solution {\n  public int numSplits(String s) {\n    int[] l = new int[26];\n    int[] r = new int[26];\n    int ans = 0;\n    int cl = 0;\n    int cr = 0;\n    for (char c : s.toCharArray())\n      if (++r[c - 'a'] == 1) ++cr;\n    for (char c : s.toCharArray()) {\n      if (++l[c - 'a'] == 1) ++cl;\n      if (--r[c - 'a'] == 0) --cr;\n      if (cl == cr) ++ans;\n    }\n    return ans;\n  }\n}\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution:\n  def numSplits(self, s: str) -> int:\n    s = [ord(c) - ord('a') for c in s]\n    l, r = [0] * 26, [0] * 26\n    cl, cr, ans = 0, 0, 0\n    for c in s:\n      r[c] += 1\n      if r[c] == 1: cr += 1\n    for c in s:\n      l[c] += 1\n      r[c] -= 1\n      if l[c] == 1: cl += 1\n      if r[c] == 0: cr -= 1\n      if cl == cr: ans += 1\n    return ans\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;s, a&nbsp;split is called&nbsp;good&nbsp;if you can split&nbsp;s&nbsp;into 2&nbsp;non-empty strings&nbsp;p&nbsp;and&nbsp;q&nbsp;where its concatenation is equal to&nbsp;s&nbsp;and the number of distinct letters in&nbsp;p&nbsp;and&nbsp;q&nbsp;are the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[476],"tags":[24,177,215],"class_list":["post-7143","post","type-post","status-publish","format-standard","hentry","category-sliding-window","tag-frequency","tag-medium","tag-sliding-window","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7143","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=7143"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7143\/revisions"}],"predecessor-version":[{"id":7145,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7143\/revisions\/7145"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}