All Permutations of Subsets without duplication
Last updated
Last updated
class Solution(object):
def allPermutationsOfSubsets(self, s):
"""
input: string set
return: string[]
"""
# write your solution here
# tree level = the position we need to fill
# [, , , ]
# [a, , ,] [b, , ,] [c, , ,] [c, , ,]
# [a,b,,], [a,c,,] [a,,,] [b, a, ,], [b,c, ,]...
#
#
action = list(s)
sol = []
sols = set()
self.bt(sol, sols, action, 0)
return sols
def bt(self,sol, sols, action, pos):
if pos == len(action):
res = "".join(sol[:])
# if res not in sols:
# sols.append(res)
sols.add(res)
return
# add one action
for i in range(pos, len(action)):
sol.append(action[i])
# swap the selected action to the begin of the selectable array
# so that we will not select the action that already picked
action[pos], action[i] = action[i], action[pos]
self.bt(sol, sols, action, pos+1)
# swap it back
action[pos], action[i] = action[i], action[pos]
sol.pop(-1)
# don't add anything
self.bt(sol, sols, action, pos+1)