78. Subsets
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Method: Backtracking (Recursive)
This is a classic combinatorial problem that can be solved using several approaches. The most intuitive and generalizable method is backtracking.
Solution
Subsets in Python
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
result = [] # This will store all the subsets
currentSubset = [] # This stores the subset being built
def backtrack(start: int):
# 1. Add the current subset to the result list.
# A copy of currentSubset is made. In Python, `[:]` creates a shallow copy.
result.append(currentSubset[:])
# 2. Iterate through the numbers from the 'start' index to generate new subsets.
for i in range(start, len(nums)):
# a. Include the element nums[i]
currentSubset.append(nums[i])
# b. Explore further with the new element included.
# We pass 'i + 1' to ensure we only consider subsequent elements, avoiding duplicates.
backtrack(i + 1)
# c. Backtrack: remove the element nums[i] to explore subsets without it.
# This prepares currentSubset for the next iteration of the loop.
currentSubset.pop()
backtrack(0)
return result
Subsets in Java
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>(); // This will store all the subsets
List<Integer> currentSubset = new ArrayList<>(); // This stores the subset being built
backtrack(0, nums, currentSubset, result);
return result;
}
private void backtrack(int start, int[] nums, List<Integer> currentSubset, List<List<Integer>> result) {
// 1. Add the current subset to the result list.
// A new ArrayList is created from currentSubset to avoid modification by reference.
result.add(new ArrayList<>(currentSubset));
// 2. Iterate through the numbers from the 'start' index to generate new subsets.
for (int i = start; i < nums.length; i++) {
// a. Include the element nums[i]
currentSubset.add(nums[i]);
// b. Explore further with the new element included.
// We pass 'i + 1' to ensure we only consider subsequent elements, avoiding duplicates.
backtrack(i + 1, nums, currentSubset, result);
// c. Backtrack: remove the element nums[i] to explore subsets without it.
// This prepares currentSubset for the next iteration of the loop.
currentSubset.remove(currentSubset.size() - 1);
}
}
}
Subsets in C++
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> result; // This will store all the subsets
vector<int> currentSubset; // This stores the subset being built in the current recursion path
backtrack(0, nums, currentSubset, result);
return result;
}
private:
void backtrack(int start, vector<int>& nums, vector<int>& currentSubset, vector<vector<int>>& result) {
// 1. Add the current subset to the result list.
// A copy of currentSubset is made and added.
result.push_back(currentSubset);
// 2. Iterate through the numbers from the 'start' index to generate new subsets.
for (int i = start; i < nums.size(); ++i) {
// a. Include the element nums[i]
currentSubset.push_back(nums[i]);
// b. Explore further with the new element included.
// We pass 'i + 1' to ensure we only consider subsequent elements, avoiding duplicates.
backtrack(i + 1, nums, currentSubset, result);
// c. Backtrack: remove the element nums[i] to explore subsets without it.
// This prepares currentSubset for the next iteration of the loop.
currentSubset.pop_back();
}
}
};
Subsets in TypeScript
function subsets(nums: number[]): number[][] {
const result: number[][] = []; // This will store all the subsets
const currentSubset: number[] = []; // This stores the subset being built
function backtrack(start: number) {
// 1. Add the current subset to the result list.
// A copy of currentSubset is made using the spread syntax `...`.
result.push([...currentSubset]);
// 2. Iterate through the numbers from the 'start' index to generate new subsets.
for (let i = start; i < nums.length; i++) {
// a. Include the element nums[i]
currentSubset.push(nums[i]);
// b. Explore further with the new element included.
// We pass 'i + 1' to ensure we only consider subsequent elements, avoiding duplicates.
backtrack(i + 1);
// c. Backtrack: remove the element nums[i] to explore subsets without it.
// This prepares currentSubset for the next iteration of the loop.
currentSubset.pop();
}
}
backtrack(0);
return result;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.