Next

List every distinct subset of numbers with repeats

Write codeEasy15 min

Your task

A test harness runs one case for every combination of the input values it was given. Values can repeat, and it should never run the same combination twice.

Write subsets_with_dup(nums) that returns every distinct subset of nums, in a fixed order.

Input and output:

  • nums: a list of up to 12 integers, each between -100 and 100. Values may repeat.
  • Returns a list of lists of integers.

Rules:

  • A subset takes any number of the values, from none to all. Each value can be used at most as many times as it appears in nums.
  • Two subsets are the same if they hold the same values the same number of times. Return each distinct subset once.
  • The empty subset is included.
  • Each subset is sorted in ascending order.
  • Sort the list of subsets by length, shortest first. Subsets of the same length are ordered by their first value, then their second, and so on, comparing numbers by value.

Examples

subsets_with_dup(nums: list[int]) -> list[list[int]]
Example 1
Inputsubsets_with_dup([1, 2, 2])
Output[[], [1], [2], [1, 2], [2, 2], [1, 2, 2]]
Example 2
Inputsubsets_with_dup([10, 9])
Output[[], [9], [10], [9, 10]]