5520. 拆分字符串使唯一子字符串的数目最大. 字符串,搜索

给你一个字符串 s ,请你拆分该字符串,并返回拆分后唯一子字符串的最大数目。

字符串 s 拆分后可以得到若干 非空子字符串 ,这些子字符串连接后应当能够还原为原字符串。但是拆分出来的每个子字符串都必须是 唯一的 。

注意:子字符串 是字符串中的一个连续字符序列。

示例 1:

输入:s = "ababccc"
输出:5
解释:一种最大拆分方法为 ['a', 'b', 'ab', 'c', 'cc'] 。像 ['a', 'b', 'a', 'b', 'c', 'cc'] 这样拆分不满足题目要求,因为其中的 'a' 和 'b' 都出现了不止一次。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/split-a-string-into-the-max-number-of-unique-substrings
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

递归搜索全部可能

class Solution:
    def maxUniqueSplit(self, s: str) -> int:
        ans = 0
        n = len(s)

        def dfs(l, res):
            nonlocal ans 
            if l == len(s):
                ans = max(ans, len(res))
            for r in range(l, n):
                tmp = s[l : r + 1]
                if tmp not in res:
                    dfs(r + 1, res | {tmp})

        dfs(0, set())
        
        return ans
posted @ 2020-09-20 14:30  _西瓜不甜  阅读(186)  评论(2编辑  收藏  举报