{"id":7402,"date":"2020-09-21T00:16:43","date_gmt":"2020-09-21T07:16:43","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7402"},"modified":"2020-09-22T10:59:37","modified_gmt":"2020-09-22T17:59:37","slug":"leetcode-1593-split-a-string-into-the-max-number-of-unique-substrings","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-1593-split-a-string-into-the-max-number-of-unique-substrings\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1593. Split a String Into the Max Number of Unique Substrings"},"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 1593. Split a String Into the Max Number of Unique Substrings - \u5237\u9898\u627e\u5de5\u4f5c EP357\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/VEdqP8rXfAc?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>,&nbsp;return&nbsp;<em>the maximum&nbsp;number of unique substrings that the given string can be split into<\/em>.<\/p>\n\n\n\n<p>You can split string&nbsp;<code>s<\/code>&nbsp;into any list of&nbsp;<strong>non-empty substrings<\/strong>, where the concatenation of the substrings forms the original string.&nbsp;However, you must split the substrings such that all of them are&nbsp;<strong>unique<\/strong>.<\/p>\n\n\n\n<p>A&nbsp;<strong>substring<\/strong>&nbsp;is a contiguous sequence of characters within a string.<\/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 = \"ababccc\"\n<strong>Output:<\/strong> 5\n<strong>Explanation<\/strong>: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.\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 = \"aba\"\n<strong>Output:<\/strong> 2\n<strong>Explanation<\/strong>: One way to split maximally is ['a', 'ba'].\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 = \"aa\"\n<strong>Output:<\/strong> 1\n<strong>Explanation<\/strong>: It is impossible to split the string any further.\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&nbsp;&lt;= 16<\/code><\/li><li><code>s<\/code>&nbsp;contains&nbsp;only lower case English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Try all combinations.<br>Time complexity: O(2^n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Iterative\/C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maxUniqueSplit(string_view s) {\n    const int n = s.length();\n    if (n == 1) return 1;\n    size_t ans = 1;\n    unordered_set<string_view> seen;\n    for (int m = 1; m < 1 << (n - 1); ++m) {\n      if (__builtin_popcount(m) < ans) continue; \/\/ 956 ms -> 52 ms\n      bool valid = true;\n      int p = 0;\n      for (int i = 1; i <= n &#038;&#038; valid; ++i) {\n        if (m &#038; (1 << (i - 1)) || i == n) {\n          valid &#038;= seen.insert(s.substr(p, i - p)).second;\n          p = i;\n        }\n      }\n      if (valid) ans = max(ans, seen.size());\n      seen.clear();\n    }\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">DFS\/C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maxUniqueSplit(string_view s) {\n    size_t ans = 1;\n    const int n = s.length();\n    unordered_set<string_view> seen;\n    function<void(int)> dfs = [&](int p) {\n      if (p == n) {        \n        ans = max(ans, seen.size());\n        return;\n      }\n      for (int i = p; i < n; ++i) {\n        string_view ss = s.substr(p, i - p + 1);\n        if (!seen.insert(ss).second) continue;\n        dfs(i + 1);\n        seen.erase(ss);\n      }\n    };\n    dfs(0);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s,&nbsp;return&nbsp;the maximum&nbsp;number of unique substrings that the given string can be split into. You can split string&nbsp;s&nbsp;into any list of&nbsp;non-empty substrings, where 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":[44],"tags":[122,177,42],"class_list":["post-7402","post","type-post","status-publish","format-standard","hentry","category-searching","tag-combination","tag-medium","tag-search","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7402","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=7402"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7402\/revisions"}],"predecessor-version":[{"id":7410,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7402\/revisions\/7410"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}