commit 76103453bfd2e332e0aa2a8a06e91ae0eec46686
parent 1b4951b999ce8834719a4c541239519894c6baa0
Author: Andrew Laack <andrew@laack.co>
Date: Tue, 15 Jul 2025 15:16:51 -0500
Completed subsets ii
Diffstat:
1 file changed, 30 insertions(+), 0 deletions(-)
diff --git a/subsets-ii/subsets-iiV2.py b/subsets-ii/subsets-iiV2.py
@@ -0,0 +1,30 @@
+# Proper backtracking solution:
+
+
+class Solution:
+ def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
+ nums.sort()
+ subs = all_subsets([], nums)
+ subs.sort()
+ return subs
+
+
+def all_subsets(current, options):
+ if len(options) == 0:
+ return [current]
+
+ return_ls = []
+ prior = -100000
+
+ for index, option in enumerate(options):
+ if option == prior:
+ continue
+ prior = option
+ cp_ls = current.copy()
+ cp_ls.append(option)
+ results = all_subsets(cp_ls, options[index + 1 : len(options)])
+ for result in results:
+ return_ls.append(result)
+
+ return_ls.append(current)
+ return return_ls