Given an integer array nums that may contain duplicates, return all possible subsets (the power set), including the empty subset and the full array.
The solution set must not contain duplicate subsets.
Function Signature
def subsets_with_dup(nums: list[int]) -> list[list[int]]:
Rules
-
A subset may use each value at most as many times as it occurs in
nums
. Two subsets that hold the same values with the same multiplicities are the same subset, whichever copies were picked.
-
List each subset in ascending order.
-
Sort the outer list lexicographically: compare element by element, and a list that is a prefix of another comes first. For example
[]
comes before
[1]
, which comes before
[1,2]
, which comes before
[2]
.
-
This only fixes the output order so grading is deterministic. It does not change the task.
Constraints
-
1 <= len(nums) <= 10
-
-10 <= nums[i] <= 10
Examples
Input: nums = [1,2,2]
Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
The distinct subsets are [], [1], [1,2], [1,2,2], [2] and [2,2]. Subsets such as [2] that can be built from either copy of 2 are listed once.
Input: nums = [0]
Output: [[],[0]]
A single element has only two subsets: the empty subset and [0].
Input: nums = [-1,2,-1]
Output: [[],[-1],[-1,-1],[-1,-1,2],[-1,2],[2]]
The input holds two copies of -1 and one 2, so there are 3 * 2 = 6 distinct subsets. Each is written in ascending order, and [-1,-1,2] comes before [-1,2] because their second elements compare as -1 < 2.