All subsets I
Given a set of characters represented by a String, return a list containing all subsets of the characters.
Assumptions
There are no duplicate characters in the original set.
βExamples
Set = "abc", all the subsets are [ββ, βaβ, βabβ, βabcβ, βacβ, βbβ, βbcβ, βcβ]
Set = "", all the subsets are [""]
Set = null, all the subsets are []
Solution:
Recursive DFS
Recursion Tree:
_
A _
AB A B _
ABC AB AC A BC B C _Base Case: When Index == set.length; Print current
Recursive rule:
Add current letter StringBuilder
Recursive call with next letter
Delete previous addition to StringBuilder
Recursive call with next letter
Implementation:
Time Complexity: O(2^N) , O(2^N * N) if storing result into array
2^N nodes * optional: toString for storing in array
Space Complexity: O(N)
StringBuilder max length = N
Last updated
Was this helpful?