Register for our webinar

1 hour

Step 1

Step 2

Congratulations!

You have registered for our webinar

Oops! Something went wrong while submitting the form.

Step 1

Step 2

Confirmed

You are scheduled with Interview Kickstart.

Redirecting...

Oops! Something went wrong while submitting the form.

Head of Career Skills Development & Coaching

*Based on past data of successful IK students

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'`

.

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

Output:

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

- 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.

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 2^{n}, where `n`

represents the length of string `s`

.

O(2^{n} * n).

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

O(2^{n} * n).

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

O(2^{n} * n).

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

```
/*
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:

- Back-end Engineering Interview Course
- Front-end Engineering Interview Course
- Full Stack Developer Interview Course

To learn more, register for the FREE webinar.

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

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'`

.

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

Output:

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

- 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.

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 2^{n}, where `n`

represents the length of string `s`

.

O(2^{n} * n).

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

O(2^{n} * n).

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

O(2^{n} * n).

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

```
/*
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:

- Back-end Engineering Interview Course
- Front-end Engineering Interview Course
- Full Stack Developer Interview Course

To learn more, register for the FREE webinar.

**Attend our free webinar to amp up your career and get the salary you deserve.**

Hosted By

Ryan Valles

Founder, Interview Kickstart