iq.lab
Python starts when a code cell comes near or you run one
mediumBacktrackingSorting target 25 min

Subsets with repeats

You get a list nums of integers, and the same value can appear in it more than once. Return every different subset of it, each exactly once, as a list of lists. A subset keeps some of the numbers and drops the rest; it can keep none of them or all of them.

Two subsets are the same when they hold the same numbers the same number of times, in any order. From [3, 1, 3], keeping only the first 3 and keeping only the second 3 both give [3], so [3] appears once. Return the subsets in any order, and the numbers inside each subset in any order.

Example 1
Inputnums = [3, 1, 3]Output[[1, 3, 3], [1, 3], [1], [3, 3], [3], []]

The 1 is kept or dropped (2 ways), and the 3s are kept 2, 1 or 0 times (3 ways): 2 × 3 = 6 different subsets.

Example 2
Inputnums = [4, 4, 4]Output[[4, 4, 4], [4, 4], [4], []]

Three copies of one number give 4 subsets: keep 3, 2, 1 or 0 of them.

Example 3
Inputnums = []Output[[]]

An empty list still has one subset: the empty one.

Constraints
  • 0 ≤ len(nums) ≤ 25

  • -10 ≤ nums[i] ≤ 10

  • The answer has at most 5,000 different subsets.

Plan it first

Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.

⌘+Enter runs 0:00Python starts when a code cell comes near or you run one
Run examples checks the examples. Submit runs every test, including edge cases and, when the problem has one, a speed check on a large input.