{"id":679,"date":"2020-09-26T11:10:09","date_gmt":"2020-09-26T03:10:09","guid":{"rendered":"http:\/\/39.96.58.60\/?p=679"},"modified":"2022-10-18T16:38:23","modified_gmt":"2022-10-18T08:38:23","slug":"leetcode-1593-%e6%8b%86%e5%88%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%bd%bf%e5%94%af%e4%b8%80%e5%ad%90%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e6%95%b0%e7%9b%ae%e6%9c%80%e5%a4%a7","status":"publish","type":"post","link":"https:\/\/www.yatenglg.cn\/blog\/?p=679","title":{"rendered":"Leetcode 1593. \u62c6\u5206\u5b57\u7b26\u4e32\u4f7f\u552f\u4e00\u5b50\u5b57\u7b26\u4e32\u7684\u6570\u76ee\u6700\u5927"},"content":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32 <code>s<\/code> \uff0c\u8bf7\u4f60\u62c6\u5206\u8be5\u5b57\u7b26\u4e32\uff0c\u5e76\u8fd4\u56de\u62c6\u5206\u540e\u552f\u4e00\u5b50\u5b57\u7b26\u4e32\u7684\u6700\u5927\u6570\u76ee\u3002<\/p>\n<p>\u5b57\u7b26\u4e32 <code>s<\/code> \u62c6\u5206\u540e\u53ef\u4ee5\u5f97\u5230\u82e5\u5e72 <strong>\u975e\u7a7a\u5b50\u5b57\u7b26\u4e32<\/strong> \uff0c\u8fd9\u4e9b\u5b50\u5b57\u7b26\u4e32\u8fde\u63a5\u540e\u5e94\u5f53\u80fd\u591f\u8fd8\u539f\u4e3a\u539f\u5b57\u7b26\u4e32\u3002\u4f46\u662f\u62c6\u5206\u51fa\u6765\u7684\u6bcf\u4e2a\u5b50\u5b57\u7b26\u4e32\u90fd\u5fc5\u987b\u662f <strong>\u552f\u4e00\u7684<\/strong> \u3002<\/p>\n<p>\u6ce8\u610f\uff1a<strong>\u5b50\u5b57\u7b26\u4e32<\/strong> \u662f\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u4e2a\u8fde\u7eed\u5b57\u7b26\u5e8f\u5217\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>s = \"ababccc\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>5\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u4e00\u79cd\u6700\u5927\u62c6\u5206\u65b9\u6cd5\u4e3a ['a', 'b', 'ab', 'c', 'cc'] \u3002\u50cf ['a', 'b', 'a', 'b', 'c', 'cc'] \u8fd9\u6837\u62c6\u5206\u4e0d\u6ee1\u8db3\u9898\u76ee\u8981\u6c42\uff0c\u56e0\u4e3a\u5176\u4e2d\u7684 'a' \u548c 'b' \u90fd\u51fa\u73b0\u4e86\u4e0d\u6b62\u4e00\u6b21\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>s = \"aba\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>2\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u4e00\u79cd\u6700\u5927\u62c6\u5206\u65b9\u6cd5\u4e3a ['a', 'ba'] \u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>s = \"aa\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>1\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u65e0\u6cd5\u8fdb\u4e00\u6b65\u62c6\u5206\u5b57\u7b26\u4e32\u3002\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= s.length&nbsp;&lt;= 16<\/code><\/li>\n<li><code>s<\/code> \u4ec5\u5305\u542b\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd<\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u56de\u6eaf\u7b97\u6cd5\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a68 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e86100.00% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.4 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e86100.00% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u56de\u6eaf+\u526a\u679d\n\"\"\"\nclass Solution:\n    def maxUniqueSplit(self, s: str) -&gt; int:\n        self.result = 0\n\n        def backtrack(s, current):\n\n            if s == '':     # \u5982\u679c\u5b57\u7b26\u4e32\u5904\u7406\u5b8c\u6210\uff0c\u5219\u5bf9\u6bd4\u5f53\u524d\u7ed3\u679c\u957f\u5ea6\uff0c\u66f4\u65b0\u7ed3\u679c\n                if len(current) &gt; self.result:\n                    self.result = len(current)\n                return\n\n            if len(current) + len(s) &lt;= self.result:    # \u526a\u679d\uff0c\u5982\u679c\u5f53\u524d\u5269\u4f59\u5b57\u7b26\u4e32\u957f\u5ea6+ \u5f53\u524d\u5217\u8868\u957f\u5ea6\u5c0f\u4e8e \u6700\u7ec8\u7ed3\u679c\uff0c\u5219\u8df3\u51fa\n                return\n\n            for i in range(1, len(s)+1):    # \u5904\u7406\u5f53\u524d\u5269\u4f59\u7684\u5b57\u7b26\u4e32\n                if s[:i] not in current:\n                    backtrack(s[i:], current+[s[:i]])\n\n        backtrack(s, [])\n        return self.result\n\n\n\"\"\"\n\u8d85\u51fa\u65f6\u95f4\u9650\u5236\n\u601d\u8def\uff1a\n    \u56de\u6eaf\uff0c\u5217\u4e3e\u6240\u6709\u7ed3\u679c\uff0c\u7136\u540e\u5bf9\u6bd4\n\"\"\"\nclass Solution:\n    def maxUniqueSplit(self, s: str) -&gt; int:\n        result = []\n\n        def backtrack(s, current):\n            print(s, current)\n            if s == '':\n                result.append(current[:])\n                return\n            for i in range(1, len(s)+1):\n                if s[:i] not in current:\n                    backtrack(s[i:], current+[s[:i]])\n\n        backtrack(s, [])\n        result = [len(r) for r in result]\n        return max(result)<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32 s \uff0c\u8bf7\u4f60\u62c6\u5206\u8be5\u5b57\u7b26\u4e32\uff0c\u5e76\u8fd4\u56de\u62c6\u5206\u540e\u552f\u4e00\u5b50\u5b57\u7b26\u4e32\u7684\u6700\u5927\u6570\u76ee\u3002 \u5b57\u7b26\u4e32 s \u62c6\u5206\u540e\u53ef\u4ee5\u5f97\u5230\u82e5\u5e72 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/679"}],"collection":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=679"}],"version-history":[{"count":1,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/679\/revisions"}],"predecessor-version":[{"id":680,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/679\/revisions\/680"}],"wp:attachment":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}