{"id":9807,"date":"2022-09-11T08:05:35","date_gmt":"2022-09-11T15:05:35","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9807"},"modified":"2022-09-11T10:59:00","modified_gmt":"2022-09-11T17:59:00","slug":"leetcode-2405-optimal-partition-of-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-2405-optimal-partition-of-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2405.\u00a0Optimal Partition of String"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-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 2405. Optimal Partition of String- \u5237\u9898\u627e\u5de5\u4f5c EP401\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/cKjQNbcSX0s?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 string&nbsp;<code>s<\/code>, partition the string into one or more&nbsp;<strong>substrings<\/strong>&nbsp;such that the characters in each substring are&nbsp;<strong>unique<\/strong>. That is, no letter appears in a single substring more than&nbsp;<strong>once<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;number of substrings in such a partition.<\/em><\/p>\n\n\n\n<p>Note that each character should belong to exactly one substring in a partition.<\/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 = \"abacaba\"\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong>\nTwo possible partitions are (\"a\",\"ba\",\"cab\",\"a\") and (\"ab\",\"a\",\"ca\",\"ba\").\nIt can be shown that 4 is the minimum number of substrings needed.\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 = \"ssssss\"\n<strong>Output:<\/strong> 6\n<strong>Explanation:\n<\/strong>The only valid partition is (\"s\",\"s\",\"s\",\"s\",\"s\",\"s\").\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;= 10<sup>5<\/sup><\/code><\/li><li><code>s<\/code>&nbsp;consists of only English lowercase letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/09\/lc-2405-ep401-s1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/09\/lc-2405-ep401-s1.png\" alt=\"\" class=\"wp-image-9812\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/09\/lc-2405-ep401-s1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/09\/lc-2405-ep401-s1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/09\/lc-2405-ep401-s1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/09\/lc-2405-ep401-s2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/09\/lc-2405-ep401-s2.png\" alt=\"\" class=\"wp-image-9813\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/09\/lc-2405-ep401-s2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/09\/lc-2405-ep401-s2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/09\/lc-2405-ep401-s2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<p>Extend the cur string as long as possible unless a duplicate character occurs.<\/p>\n\n\n\n<p>You can use hashtable \/ array or bitmask to mark whether a character has been seen so far.<\/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 partitionString(string s) {\n    unordered_set<char> m;\n    int ans = 0;\n    for (char c : s) {\n      if (m.insert(c).second) continue;\n      m.clear();\n      m.insert(c);\n      ++ans;\n    }\n    return ans + 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\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 partitionString(string s) {\n    vector<int> m(26);\n    int ans = 0;\n    for (char c : s) {\n      if (++m[c - 'a'] == 1) continue;\n      fill(begin(m), end(m), 0);\n      m[c - 'a'] = 1;\n      ++ans;\n    }\n    return ans + 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\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 partitionString(string s) {\n    int mask = 0;\n    int ans = 0;\n    for (char c : s) {\n      if (mask & (1 << (c - 'a'))) {\n        mask = 0;        \n        ++ans;\n      }\n      mask |= 1 << (c - 'a');\n    }\n    return ans + 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s, partition the string into one or more&nbsp;substrings&nbsp;such that the characters in each substring are&nbsp;unique. That is, no letter appears in a single&#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":[621,88,82],"class_list":["post-9807","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-bitmask","tag-greedy","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9807","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=9807"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9807\/revisions"}],"predecessor-version":[{"id":9815,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9807\/revisions\/9815"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}