Subsets of all permuations
Medium
Given a string with no duplicate characters, return a list with all permutations of the string and all its subsets.
Examples
Set = “abc”, all permutations are [“”, “a”, “ab”, “abc”, “ac”, “acb”, “b”, “ba”, “bac”, “bc”, “bca”, “c”, “cb”, “cba”, “ca”, “cab”].
Set = “”, all permutations are [“”].
Set = null, all permutations are [].
Solution: DFS
Each layer one index
Each layer represents Permutation(n choose index + 1) *zero index
Perform permutation with:
swap swap
TC: O(N!)
SC:
Heap: O(N!) result array
Stack: O(N)
Last updated
Was this helpful?