26. Power Set
The problem can be found at the following link: Question Link
My Approach
We use a recursive function to generate all possible subsequences of the given string.
At each step of recursion, we have two choices: either include the current character in the subsequence or exclude it.
We recursively call the function with the next index and both choices.
We store all generated subsequences in a vector.
Finally, we sort the vector to ensure lexicographically sorted output.
Time and Auxiliary Space Complexity
Time Complexity:
O(2^N * N)
, whereN
is the length of the input string. This is because there are2^N
subsets, and for each subset, there can be up to N characters.Auxiliary Space Complexity:
O(2^N * N)
, the space required to store all subsets.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the getlost01/gfg-potd repository.
Last updated