Python Easy to Understand List Solution by Changing Start, End Indexes
Consider the example [1, 5, 3, 3]\n\n1. Sort nums to place duplicates next to each other [1, 3, 3, 5]\n2. Start with an empty set: [[]]\n3. Add the first number
Consider the example [1, 5, 3, 3]\n\n1. Sort nums to place duplicates next to each other [1, 3, 3, 5]\n2. Start with an empty set: [[]]\n3. Add the first number (1) to all the existing subsets to create new subsets: [[], [1]];\n4. Add the second number (3) to all the existing subsets: [[], [1], [3], [1,3]].\n5. The next number (3) is a duplicate. If we add it to all existing subsets we will get:\n    [[], [1], [3], [1,3], [3], [1,3], [3,3], [1,3,3]]\n6. To handle this instead of adding (3) to all the existing subsets, we only add it to the new subsets which were created in the previous (4th) step ([3],[1,3]):\n    [[], [1], [3], [1,3], [3,3], [1,3,3]]\n\t\n7. Finally, add the forth number (5) to all the existing subsets: [[], [1], [3], [1,3], [3,3], [1,3,3], [5], [1,5], [3,5], [1,3,5], [3,3,5], [1,3,3,5]]\n\n```\nclass Solution:\n    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:\n        if not nums:\n            return []\n        \n        subsets = []\n        \n        #Sorting nums so that duplicates are placed next to each other\n        nums.sort()\n        \n        #Adding empty element to subsets\n        subsets.append([])\n        \n        #Manipulating start, end indices\n        startIndex, endIndex = 0, 0\n        \n        for i in range(len(nums)):\n            #Initialize startIndex as 0 for every incoming number (normal unless duplicate)\n            startIndex = 0\n            \n            #Checking duplicate condition\n            if i > 0 and nums[i] == nums[i - 1]:\n                #Update startIndex to only consider 2nd half, subsets added in the previous step\n                startIndex = endIndex + 1\n                \n            #Set endIndex till the end of subsets \n            endIndex = len(subsets) - 1\n            \n            #Adding current Number to all eligible subsets\n            for j in range(startIndex, endIndex + 1):\n                #Selecting subset to add element to \n                set1 = list(subsets[j])\n                set1.append(nums[i])\n                subsets.append(set1)\n        \n        return subsets\n```\n\nTime Complexity: O(N * 2^N) [Number of subsets double at each step for every element N (if not duplicate)]\nSpace Complexity: O(N * 2^N) [O(2^N) subsets taking O(N) space]
['Python']