![]() |
VOOZH | about |
This article is about generating Power set in lexicographical order.
Examples :
Input : abc
Output : a ab abc ac b bc c
The idea is to sort array first. After sorting, one by one fix characters and recursively generates all subsets starting from them. After every recursive call, we remove last character so that next permutation can be generated.
Implementation:
a ab b c ca cab cb
Time Complexity: O(n*2n)
Auxiliary Space: O(1)
Method (binary numbers)
The idea is to use binary numbers to generate the power set of a given set of elements in lexicographical order
a ab abc ac b bc c
Time complexity :O(2^n * n), where n is the length of the input set.
Space complexity :O(2^n * n), since the output list of subsets can potentially contain 2^n elements