{"id":525,"date":"2020-09-26T10:52:04","date_gmt":"2020-09-26T02:52:04","guid":{"rendered":"http:\/\/39.96.58.60\/?p=525"},"modified":"2022-10-18T16:38:55","modified_gmt":"2022-10-18T08:38:55","slug":"leetcode-1239-%e4%b8%b2%e8%81%94%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e6%9c%80%e5%a4%a7%e9%95%bf%e5%ba%a6","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=525","title":{"rendered":"Leetcode 1239. \u4e32\u8054\u5b57\u7b26\u4e32\u7684\u6700\u5927\u957f\u5ea6"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4 <code>arr<\/code>\uff0c\u5b57\u7b26\u4e32 <code>s<\/code> \u662f\u5c06 <code>arr<\/code> \u67d0\u4e00\u5b50\u5e8f\u5217\u5b57\u7b26\u4e32\u8fde\u63a5\u6240\u5f97\u7684\u5b57\u7b26\u4e32\uff0c\u5982\u679c <code>s<\/code> \u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\u90fd\u53ea\u51fa\u73b0\u8fc7\u4e00\u6b21\uff0c\u90a3\u4e48\u5b83\u5c31\u662f\u4e00\u4e2a\u53ef\u884c\u89e3\u3002<\/p>\n<p>\u8bf7\u8fd4\u56de\u6240\u6709\u53ef\u884c\u89e3 <code>s<\/code> \u4e2d\u6700\u957f\u957f\u5ea6\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>arr = [\"un\",\"iq\",\"ue\"]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>4\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u6240\u6709\u53ef\u80fd\u7684\u4e32\u8054\u7ec4\u5408\u662f \"\",\"un\",\"iq\",\"ue\",\"uniq\" \u548c \"ique\"\uff0c\u6700\u5927\u957f\u5ea6\u4e3a 4\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>arr = [\"cha\",\"r\",\"act\",\"ers\"]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>6\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u53ef\u80fd\u7684\u89e3\u7b54\u6709 \"chaers\" \u548c \"acters\"\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>arr = [\"abcdefghijklmnopqrstuvwxyz\"]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>26\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= arr.length &lt;= 16<\/code><\/li>\n<li><code>1 &lt;= arr[i].length &lt;= 26<\/code><\/li>\n<li><code>arr[i]<\/code>&nbsp;\u4e2d\u53ea\u542b\u6709\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd<\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u4f4d\u8fd0\u7b97\u3001 \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\uff1a156 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8634.91% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a18.9 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e867.28% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u56de\u6eaf\n\"\"\"\nclass Solution:\n    def maxLength(self, arr: List[str]) -&gt; int:\n        results = []    # \u7528\u4e8e\u5b58\u653e\u6240\u6709\u7ed3\u679c\n        n = len(arr)\n\n        def backtrack(begin, current:str):  # \u8d77\u59cb\u4f4d\u7f6e\uff0c\u5f53\u524d\u5408\u5e76\u5b57\u7b26\u4e32\n            results.append(current) # \u628a\u5f53\u524d\u7ed3\u679c\u5b58\u5165\u6700\u7ec8\u7ed3\u679c\u4e2d\n            if begin == n:  # \u5982\u679c\u904d\u5386\u7ed3\u675f\uff0c\u5219\u7ed3\u675f\n                return\n            for i in range(begin, n):   # \u4ece\u8d77\u59cb\u4f4d\u7f6e\u4e00\u76f4\u5411\u540e\u904d\u5386\n                if len(set(current+arr[i])) != len(current+arr[i]): # \u5982\u679c\u5f53\u524d\u5b57\u7b26\u4e32\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff0c\u5219\u8df3\u51fa\uff0c\u7ee7\u7eed\u4e0b\u4e00\u4e2a\n                    continue\n                backtrack(i, current+arr[i])    # \u5904\u7406\u4e0b\u4e00\u4e2a\u5b57\u7b26\u4e32\n                # \u56e0\u4e3a\u662f\u5b57\u7b26\u4e32\uff0c\u4e14\uff0c\u4f20\u5165\u7684\u662fcurrent+arr[i], \u8fd9\u91cc\u4e0d\u9700\u8981\u8fdb\u884c\u989d\u5916\u5904\u7406\n        backtrack(0, '')\n        result = 0\n        for r in results:\n            if len(r) &gt; result:\n                result = len(r)\n        return result\n\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a156 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8634.91% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.5 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8696.12% \u7684\u7528\u6237\n\n\u4ee3\u7801\u7ecf\u8fc7\u7cbe\u7b80\u540e\n\"\"\"\nclass Solution:\n    def maxLength(self, arr: List[str]) -&gt; int:\n        self.result = 0\n        n = len(arr)\n\n        def backtrack(begin, current:str):\n\n            if len(current) &gt; self.result:\n                self.result = len(current)\n\n            for i in range(begin, n):\n                if len(set(current+arr[i])) != len(current+arr[i]):\n                    continue\n                backtrack(i, current+arr[i])\n\n        backtrack(0, '')\n\n        return self.result<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4 arr\uff0c\u5b57\u7b26\u4e32 s \u662f\u5c06 arr \u67d0\u4e00\u5b50\u5e8f\u5217\u5b57\u7b26\u4e32\u8fde\u63a5\u6240\u5f97\u7684\u5b57\u7b26\u4e32\uff0c\u5982\u679c s \u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57&#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":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/525"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=525"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/525\/revisions"}],"predecessor-version":[{"id":526,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/525\/revisions\/526"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=525"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}