leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit af538cc2d8cdfdf28348731cd00125c614080bb9
parent f0d10bbd1ce2281b7abfaa4e6af28590027c977d
Author: Andrew Laack <andrew@laack.co>
Date:   Mon, 14 Jul 2025 09:06:04 -0500

Completed subsets ii

Diffstat:
Asubsets-ii/subsets-iiV1.py | 39+++++++++++++++++++++++++++++++++++++++
1 file changed, 39 insertions(+), 0 deletions(-)

diff --git a/subsets-ii/subsets-iiV1.py b/subsets-ii/subsets-iiV1.py @@ -0,0 +1,39 @@ +# IDEA: + +# return the powerset of the set defined by the list of nums +# given that a mathematical set does not contain duplicate elements, +# we remove said duplicates by converting nums into a python set +# which contains has the same property + +# we then recursively add or don't add any element at each step, +# returning a list of lists back up the stack and to the consumer +# of the method. + + +class Solution: + def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: + nums.sort() + perms = recurse(nums, 0, []) + res = {str(str_perm): str_perm for str_perm in perms} + return list(res.values()) + + +def recurse(nums, index, current): + + if index == len(nums): + return [current] + + return_ls = [] + with_current = current.copy() + with_current.append(nums[index]) + + res1 = recurse(nums, index + 1, with_current) + + res2 = recurse(nums, index + 1, current) + + for res in res2: + return_ls.append(res) + for res in res1: + return_ls.append(res) + + return return_ls