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"]
s = "a"
, arrays ["", "a"]
and ["a", ""]
both will be accepted.s = "xy"
, array ["", "x", "y", "xy"]
will be accepted, but ["", "x", "y", "yx"]
will not be accepted.Constraints:
s
<= 19s
only contains distinct lowercase English letters.We have provided 2 solutions:
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 2n, where n
represents the length of string s
.
O(2n * n).
As we will generate 2n strings of length O(n).
O(2n * n).
As we will store 2n strings of length O(n) in the output array to be returned.
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).
/*
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:
To learn more, register for the FREE webinar.
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"]
s = "a"
, arrays ["", "a"]
and ["a", ""]
both will be accepted.s = "xy"
, array ["", "x", "y", "xy"]
will be accepted, but ["", "x", "y", "yx"]
will not be accepted.Constraints:
s
<= 19s
only contains distinct lowercase English letters.We have provided 2 solutions:
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 2n, where n
represents the length of string s
.
O(2n * n).
As we will generate 2n strings of length O(n).
O(2n * n).
As we will store 2n strings of length O(n) in the output array to be returned.
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).
/*
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:
To learn more, register for the FREE webinar.
Attend our free webinar to amp up your career and get the salary you deserve.