Advertisement
Iam_Sandeep

Print all Subsets

Jul 19th, 2022
1,039
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. #unique subsets
  2. class Solution:
  3.     def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
  4.         ans=[]
  5.         st=[]
  6.         n=len(nums)
  7.         nums.sort()
  8.         def dfs(i):
  9.             if i>=n:
  10.                 print(st,ans)
  11.                 ans.append(st[:])
  12.                 return
  13.             st.append(nums[i])
  14.             dfs(i+1)
  15.             st.pop()
  16.             while i+1<n and nums[i]==nums[i+1]:
  17.                 i=i+1
  18.             dfs(i+1)
  19.             return
  20.            
  21.         dfs(0)
  22.         return ans
  23. '''
  24. #Repeated subsets
  25. Just above code remove that while loop
  26. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement