Skip to content

Commit ebcad0b

Browse files
committed
Subsets-II
1 parent 1d0f980 commit ebcad0b

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

Backtracking/90-Subsets-II.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'''
2+
Subsets II
3+
Given an integer array nums that may contain duplicates, return all possible subsets (the power set).
4+
5+
The solution set must not contain duplicate subsets. Return the solution in any order.
6+
7+
8+
9+
Example 1:
10+
11+
Input: nums = [1,2,2]
12+
Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
13+
'''
14+
15+
16+
def subsetsWithDup(nums):
17+
res = []
18+
19+
nums.sort()
20+
21+
def backtrack(i, subset):
22+
if i == len(nums):
23+
res.append(subset[::])
24+
return
25+
26+
# All subsets that include nums[i]
27+
subset.append(nums[i])
28+
backtrack(i+1, subset)
29+
subset.pop()
30+
31+
# All subsets that don't include nums[i]
32+
while i+1 < len(nums) and nums[i] == nums[i+1]:
33+
i += 1
34+
backtrack(i+1, subset)
35+
backtrack(0, [])
36+
return res
37+
38+
#T:O(n.2^n)
39+
#S:O(n)

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# leetcode-solutions-python
2+
Python solutions to the leetcode problems
23

4+
## Notes
5+
Check the notes for the explaination - [Notes](https://stingy-shallot-4ea.notion.site/SeanPrashad-Leetcode-Patterns-7b700a3aa52543a4ace4dbdeb718b8cc)
6+
7+
## Coding solution
38
- [x] [Backtracking](Backtracking)
49
- [x] [Word Search](Backtracking/79-Word-Search.py)
510
- [x] [Letter Case Permutation](Backtracking/784-letter-case-permutation.py)
611
- [x] [Subsets](Backtracking/78-Subsets.py)
12+
- [x] [Subsets-II](Backtracking/78-Subsets.py)
713

0 commit comments

Comments
 (0)