生成没有重复的子集组合?

时间:2018-07-03 21:36:08

标签: algorithm depth-first-search subset-sum

给出一个可能具有重复数字的数字列表,返回所有可能的子集组合。

如果S = [2,2,2],则解决方案是:

[[[[2],[2],[2]],[[2],[2,2]],[[2,2,2]]]

---实际上有一系列问题,上面是Split String III ---

分割字符串

给出一个字符串,您可以选择将字符串分割为一个字符或两个相邻字符,然后使该字符串仅由一个字符或两个字符组成。输出所有可能的结果。

Example
Given the string "123"
return [["1","2","3"],["12","3"],["1","23"]]

分割字符串II

给出一个字符串,您可以选择将一个字符串分割为一个字符或任何相邻的字符,然后使该字符串由这些字符组成。输出所有可能的结果。

Example
Given the string "123"
return [['1', '2', '3'], ['1', '23'], ['12', '3'], ['123']]

分割字符串III

给出一个字符串,您可以选择将一个字符串分割为一个字符或任何相邻的字符,然后使该字符串由这些字符组成。输出所有可能的结果。字符串可能包含重复的数字,以减少重复的结果,并按字典顺序保留它们。

Example
Given the string "222"
return [['2', '2', '2'], ['2', '22'], ['222']]

1 个答案:

答案 0 :(得分:0)

为参考提供一个分析器,使用split作为标记来减少重复的dfs。

   def subsets4(self, S):
        res = []
        split = [False for _ in range(len(S))]
        split_num = {}
        self.count = 0 
        def dfs(start_index, tmp):
            self.count += 1 
            if start_index == len(S):
                res.append(tmp)
                return 
            for i in range(start_index,len(S)):
                if (i >=2 and S[i-1] == S[i] and split[i-2] == False and split_num[S[i]] != False):
                    continue
                split[i] = True
                if S[start_index] == S[i]:
                    split_num[S[i]] = True
                dfs(i + 1, tmp + [S[start_index:i+1]])
                split_num[S[i]] = False
                split[i] = False
        S.sort()
        dfs(0, [])
        print 'dfs count:', self.count,
        return res 

>>> subsets4([2, 2, 2, 2, 2])
>>> dfs count: 20 [[[2], [2], [2], [2], [2]], [[2], [2], [2], [2, 2]], [[2], [2], [2, 2, 2]], [[2], [2, 2], [2, 2]], [[2], [2, 2, 2, 2]], [[2, 2], [2, 2, 2]], [[2, 2, 2], [2, 2]], [[2, 2, 2, 2, 2]]]

------------------还附带了分割字符串答案----------------

Class Solution(object):


    def splitString(self, S):
        if s is None:
            return []

        res = []
        # dfs helper to search solution for index + 1, index + 2
        def dfs(start, tmp):
            if start == len(S):
                res.append(tmp)
            end = min(start + 2, len(S))
            for i in range(start, end):
                dfs(i+1, tmp + [S[start:i+1]])

        dfs(0, [])
        return res


    def splitString2(self, S):
        if s is None:
            return []

        res = []
        # dfs helper to search solution for index + 1, index + 2
        def dfs(start, tmp):
            if start == len(S):
                res.append(tmp)
            end = len(S)
            for i in range(start, end):
                dfs(i+1, tmp + [S[start:i+1]])

        dfs(0, [])
        return res

    def splitString3(self, S):
        res = []
        split = [False for _ in range(len(S))]
        split_num = {}

        def dfs(start, tmp):
            if start == len(S):
                res.append(tmp)
                return 
            for i in range(start,len(S)):
                if (i >=2 and S[i-1] == S[i] and split[i-2] == False and split_num[S[i]] != False):
                    continue
                split[i] = True
                if S[start] == S[i]:
                    split_num[S[i]] = True
                dfs(i + 1, tmp + [S[start:i+1]])
                split_num[S[i]] = False
                split[i] = False

        dfs(0, [])
        return res