✅ C++ clean code | Subset - II | Brute Force and Optimized
1. Brute Force : Generate all subsets (Subset-II) \n\nTime Complexity : O(2^n*( klog(x) )).2^n\nSpace Complexity : O(2^n * k)\n\nclass Solution{\npublic:\n    \
**1. Brute Force : Generate all subsets (Subset-II)** \n\n**Time Complexity :** **`O(2^n*( klog(x) )).2^n`**\n**Space Complexity :** **`O(2^n * k)`**\n```\nclass Solution{\npublic:\n    \n    void solve(vector<int>&nums, int i, vector<int>ds, set<vector<int>>&st) \n    {\n        if(i == nums.size()) \n        {\n            sort(ds.begin(), ds.end());\n            st.insert(ds);\n            return;\n        }\n        \n        ds.push_back(nums[i]);\n        solve(nums, i + 1, ds, st);\n        ds.pop_back();\n        solve(nums, i + 1, ds, st);\n    }\n    \n    vector<vector<int>>subsetsWithDup(vector<int>& nums) \n    {\n        vector<vector<int>>ans;\n        set<vector<int>>st;\n        vector<int>ds;\n        \n        solve(nums, 0, ds, st);\n        for(auto &v : st) \n            ans.push_back(v);\n        return ans;\n    }\n};\n```\n\n**2. More Optimized - (Subset-II)**  \n\n**Time Complexity :** **`O(k * 2^n)`**\n**Space Complexity :** **`O(2^n * k)`**\n```\nclass Solution {\npublic:\n    \n    void solve(int ind, vector<int>&nums, vector<int>&path, vector<vector<int>>&paths)\n    {\n        paths.push_back(path);\n        \n        for(int i=ind;i<nums.size();i++)\n        {    \n            if(i!=ind and nums[i] == nums[i-1]) continue; // To avoid duplicate subsets\n            \n            path.push_back(nums[i]);\n            solve(i+1, nums, path, paths);\n            path.pop_back();\n        }\n    }\n    \n    vector<vector<int>> subsetsWithDup(vector<int>& nums) \n    {    \n        vector<vector<int>>paths;\n        vector<int>path;\n        sort(nums.begin(), nums.end()); // sorting is most important --- so that order of duplicate subsets is same\n        solve(0, nums, path, paths);    // [1,4,4] == [4,1,4] , both are same so no need to include both in answer\n        return paths; \n    }\n};\n```\n***Thanks for Upvoting !***\n\uD83D\uDE42
['Backtracking', 'Depth-First Search', 'Recursion', 'C', 'Ordered Set', 'C++']