I am trying to create all subset of a given string recursively.
Given string = 'aab', we generate all subsets for the characters being distinct.
The answer is: ["", "b", "a", "ab", "ba", "a", "ab", "ba", "aa", "aa", "aab", "aab", "aba", "aba", "baa", "baa"]
.
I have been looking at several solutions such as this one
but I am trying to make the function accept a single variable- only the string and work with that, and can't figure out how.
I have been also looking at this solution of a similar problem, but as it deals with lists and not strings I seem to have some trouble transforming that to accept and generate strings.
Here is my code, in this example I can't connect the str to the list. Hence my question.
I edited the input and the output.
def gen_all_strings(word):
if len(word) == 0:
return ''
rest = gen_all_strings(word[1:])
return rest + [[ + word[0]] + dummy for dummy in rest]