Subsets II (Leetcode 90)

Problem Link: https://leetcode.com/problems/subsets-ii/

class Solution:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
        
        ans=[]
        
        def recursion(nums,tempAns):
            
            if(len(nums)==0):
                ans.append(tempAns)
                return
            
      
            ele=nums[0]
            temp=tempAns[:]
            temp.append(nums[0])
            
            # Considering the element
            recursion(nums[1:],temp)
            
            # Ignoring the element
            
            # When ignoring, ignoring all the other occuences 
            # of that element also
            i=0
            while(i+1<len(nums) and nums[i]==nums[i+1]):
                i+=1
            recursion(nums[i+1:],tempAns)
                
                 
        nums.sort()
        recursion(nums,[])
        return ans

Last updated