-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateSubSets.js
More file actions
executable file
·49 lines (34 loc) · 1.18 KB
/
generateSubSets.js
File metadata and controls
executable file
·49 lines (34 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// function to generate all subsets of a given set using recursion
// what is subset ?
// A subset is a set that contains some or all elements of another set
// For example, the subsets of the set {1, 2} are {}, {1}, {2}, and {1, 2}
function generateSubSets(nums) {
const result = [];
function backtrack(index, current) {
if (index === nums.length) {
result.push([...current]);
return;
}
// include
current.push(nums[index]);
backtrack(index + 1, current);
// exclude
current.pop();
backtrack(index + 1, current);
}
backtrack(0, []);
return result;
}
// time complexity: O(2^n)
// space complexity: O(n) due to call stack
// test the function
console.log(generateSubSets([1, 2]));
// Output: [ [], [ 1 ], [ 1, 2 ], [ 2 ] ]
console.log(generateSubSets([1, 2, 3]));
// Output: [ [], [ 1 ], [ 1, 2 ], [ 1, 2, 3 ], [ 1, 3 ], [ 2 ], [ 2, 3 ], [ 3 ] ]
console.log(generateSubSets(["a", "b"]));
// Output: [ [], [ 'a' ], [ 'a', 'b' ], [ 'b' ] ]
console.log(generateSubSets(["x", "y", "z"]));
// Output: [ [], [ 'x' ], [ 'x', 'y' ], [ 'x', 'y', 'z' ], [ 'x', 'z' ], [ 'y' ], [ 'y', 'z' ], [ 'z' ] ]
console.log(generateSubSets([]));
// Output: [ [] ]