 Register for our webinar

### How to Nail your next Technical Interview

1 hour 1
Enter details
2
Select webinar slot
*Invalid Name
*Invalid Name  Step 1  Step 2
Congratulations!
You have registered for our webinar Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
*All webinar slots are in the Asia/Kolkata timezone  Step 1  Step 2 Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.  ## You may be missing out on a 66.5% salary hike* ### Nick Camilleri

Head of Career Skills Development & Coaching
*Based on past data of successful IK students Help us know you better!

## How many years of coding experience do you have?

Oops! Something went wrong while submitting the form.  ## FREE course on 'Sorting Algorithms' by Omkar Deshpande (Stanford PhD, Head of Curriculum, IK)

Oops! Something went wrong while submitting the form.  # Generate All Subsets Of A Set Problem Statement

Generate ALL possible subsets of a given set. The set is given in the form of a string `s` containing distinct lowercase characters `'a' - 'z'`.

## Example

``````{
"s": "xy"
}
``````

Output:

``````["", "x", "y", "xy"]
``````

## Notes

• Any set is a subset of itself.
• Empty set is a subset of any set.
• Output contains ALL possible subsets of given string.
• Order of strings in the output does not matter. E.g. `s = "a"`, arrays `["", "a"]` and `["a", ""]` both will be accepted.
• Order of characters in any subset must be same as in the input string. For `s = "xy"`, array `["", "x", "y", "xy"]` will be accepted, but `["", "x", "y", "yx"]` will not be accepted.

Constraints:

• 0 <= length of `s` <= 19
• `s` only contains distinct lowercase English letters.

We have provided 2 solutions:

• Recursive Solution : other_solution.cpp.
• Iterative Solution : Generate All Subsets Of A Set Solution: Optimal.

Have a look at both the solutions.

# optimal_solution.cpp

Both solutions are valid, but the recursive solution is slightly slower because of function calls and variable passing.

Also you should observe that the number of subsets will always be a power of 2, specifically 2n, where `n` represents the length of string `s`.

## Time Complexity

O(2n * n).

As we will generate 2n strings of length O(n).

## Auxiliary Space Used

O(2n * n).

As we will store 2n strings of length O(n) in the output array to be returned.

## Space Complexity

O(2n * n).

As auxiliary space used is O(2n * n) and input is O(n), hence O(2n * n) + O(n) = O(2n * n).

## Code For Generate All Subsets Of A Set Solution: Optimal

``````/*
Asymptotic complexity in terms of the length of \`s\` \`n\`:
* Time: O(2^n * n).
* Auxiliary space: O(2^n * n).
* Total space: O(2^n * n).
*/

vector<string> generate_all_subsets(string &s) {
int n = s.length();
vector<string> all_subsets;
// Base case when n = 0 i.e. s = "".
all_subsets.push_back("");
/*
Suppose s = "xyz".
Now try to find pattern in following steps (think how any step is related to previous step!):
- [""]
- ["", "x"]
- ["", "x", "y", "xy"]
- ["", "x", "y", "xy", "z", "xz", "yz", "xyz"]
Let me explain what we have done in last step.
First take array from previous step, that is ["", "x", "y", "xy"], append 'z' to each string,
that is ["z", "xz", "yz", "xyz"], now merge it with array in previous step!
*/
for (int i = 1; i <= n; i++) {
int old_len = all_subsets.size();
for (int j = 0; j < old_len; j++) {
/*
Note that we are doing push_back that is adding value at the end of array, so we
already have the old values stored in it.
*/
all_subsets.push_back(all_subsets[j] + s[i - 1]);
}
}
return all_subsets;
}

``````

We hope that these solutions to generate all subsets of a set problem have helped you level up your coding skills. You can expect problems like these at top tech companies like Amazon and Google.

If you are preparing for a tech interview at FAANG or any other Tier-1 tech company, register for Interview Kickstart's FREE webinar to understand the best way to prepare.

Interview Kickstart offers interview preparation courses taught by FAANG+ tech leads and seasoned hiring managers. Our programs include a comprehensive curriculum, unmatched teaching methods, and career coaching to help you nail your next tech interview.

We offer 18 interview preparation courses, each tailored to a specific engineering domain or role, including the most in-demand and highest-paying domains and roles, such as:

### Try yourself in the Editor

Note: Input and Output will already be taken care of.

# Generate All Subsets Of A Set Problem Statement

Generate ALL possible subsets of a given set. The set is given in the form of a string `s` containing distinct lowercase characters `'a' - 'z'`.

## Example

``````{
"s": "xy"
}
``````

Output:

``````["", "x", "y", "xy"]
``````

## Notes

• Any set is a subset of itself.
• Empty set is a subset of any set.
• Output contains ALL possible subsets of given string.
• Order of strings in the output does not matter. E.g. `s = "a"`, arrays `["", "a"]` and `["a", ""]` both will be accepted.
• Order of characters in any subset must be same as in the input string. For `s = "xy"`, array `["", "x", "y", "xy"]` will be accepted, but `["", "x", "y", "yx"]` will not be accepted.

Constraints:

• 0 <= length of `s` <= 19
• `s` only contains distinct lowercase English letters.

We have provided 2 solutions:

• Recursive Solution : other_solution.cpp.
• Iterative Solution : Generate All Subsets Of A Set Solution: Optimal.

Have a look at both the solutions.

# optimal_solution.cpp

Both solutions are valid, but the recursive solution is slightly slower because of function calls and variable passing.

Also you should observe that the number of subsets will always be a power of 2, specifically 2n, where `n` represents the length of string `s`.

## Time Complexity

O(2n * n).

As we will generate 2n strings of length O(n).

## Auxiliary Space Used

O(2n * n).

As we will store 2n strings of length O(n) in the output array to be returned.

## Space Complexity

O(2n * n).

As auxiliary space used is O(2n * n) and input is O(n), hence O(2n * n) + O(n) = O(2n * n).

## Code For Generate All Subsets Of A Set Solution: Optimal

``````/*
Asymptotic complexity in terms of the length of \`s\` \`n\`:
* Time: O(2^n * n).
* Auxiliary space: O(2^n * n).
* Total space: O(2^n * n).
*/

vector<string> generate_all_subsets(string &s) {
int n = s.length();
vector<string> all_subsets;
// Base case when n = 0 i.e. s = "".
all_subsets.push_back("");
/*
Suppose s = "xyz".
Now try to find pattern in following steps (think how any step is related to previous step!):
- [""]
- ["", "x"]
- ["", "x", "y", "xy"]
- ["", "x", "y", "xy", "z", "xz", "yz", "xyz"]
Let me explain what we have done in last step.
First take array from previous step, that is ["", "x", "y", "xy"], append 'z' to each string,
that is ["z", "xz", "yz", "xyz"], now merge it with array in previous step!
*/
for (int i = 1; i <= n; i++) {
int old_len = all_subsets.size();
for (int j = 0; j < old_len; j++) {
/*
Note that we are doing push_back that is adding value at the end of array, so we
already have the old values stored in it.
*/
all_subsets.push_back(all_subsets[j] + s[i - 1]);
}
}
return all_subsets;
}

``````

We hope that these solutions to generate all subsets of a set problem have helped you level up your coding skills. You can expect problems like these at top tech companies like Amazon and Google.

If you are preparing for a tech interview at FAANG or any other Tier-1 tech company, register for Interview Kickstart's FREE webinar to understand the best way to prepare.

Interview Kickstart offers interview preparation courses taught by FAANG+ tech leads and seasoned hiring managers. Our programs include a comprehensive curriculum, unmatched teaching methods, and career coaching to help you nail your next tech interview.

We offer 18 interview preparation courses, each tailored to a specific engineering domain or role, including the most in-demand and highest-paying domains and roles, such as:

## Worried About Failing Tech Interviews?

Attend our free webinar to amp up your career and get the salary you deserve. Hosted By
Ryan Valles
Founder, Interview Kickstart Accelerate your Interview prep with Tier-1 tech instructors 360° courses that have helped 14,000+ tech professionals 100% money-back guarantee*