<p>Given a collection of candidate numbers (<code>candidates</code>) and a target number (<code>target</code>), find all unique combinations in <code>candidates</code>&nbsp;where the candidate numbers sum to <code>target</code>.</p>

<p>Each number in <code>candidates</code>&nbsp;may only be used <strong>once</strong> in the combination.</p>

<p><strong>Note:</strong>&nbsp;The solution set must not contain duplicate combinations.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> candidates = [10,1,2,7,6,1,5], target = 8
<strong>Output:</strong> 
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> candidates = [2,5,2,1,2], target = 5
<strong>Output:</strong> 
[
[1,2,2],
[5]
]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;=&nbsp;candidates.length &lt;= 100</code></li>
	<li><code>1 &lt;=&nbsp;candidates[i] &lt;= 50</code></li>
	<li><code>1 &lt;= target &lt;= 30</code></li>
</ul>

class Solution {
public:

    // void combinationSum2(unordered_map<<int,int>>& candidates, int target, std::unordered_map<std::int, int>::iterator iter, vector<int> &temp, vector<vector<int>> &res)
    void combinationSum2(std::unordered_map<int, int>& candidates, int target, std::unordered_map<int, int>::iterator iter, std::vector<int> &temp, std::vector<std::vector<int>> &res)
    {   
        if (target<0)
            return; //prune

        if (iter == candidates.end())
            return;
        

        auto candidate = iter->first;
        auto count = iter->second;
        int j;
        for(j = 0; j <= count; j++)
        {
            auto newtarget = target - j * candidate;
            if(j>0)
                temp.push_back(candidate);

            // cout << "--"<<newtarget<<endl;
            // for(auto t: temp)
            //     cout<<t<<" ";
            // cout<<endl;

            if (newtarget == 0)
            {
                res.push_back(vector<int>(temp));
                for(int k = 0; k< j;k++)
                    temp.pop_back();
                return;
            }
            else
            {
                // combinationSum2(candidates, newtarget, ++iter, temp, res); //++iter, first ++ and then pass to function. This change the iteration
                
                combinationSum2(candidates, newtarget, std::next(iter), temp, res);
            }
        }
        for(int j = 0; j< count;j++)
            temp.pop_back();
    }

    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        unordered_map<int,int> counts;
        for(auto candidate:candidates)
            counts[candidate]+=1;
        

        vector<int> temp; vector<vector<int>> res;
        combinationSum2(counts, target, counts.begin(), temp, res);
        return res;
        
    }
};



// //this solution has duplicates

// class Solution {
// public:

//     void combinationSum2(vector<int>& candidates, int target, int i, vector<int> &temp, vector<vector<int>> &res)
//     {   
        
//         if (i == candidates.size())
//             return;
        
//         //don't select this element
//         combinationSum2(candidates, target, i+1, temp, res);

//         //select this element
//         temp.push_back(candidates[i]);
//         target -= candidates[i];

//         cout << "--"<<target<<endl;
//         for(auto t: temp)
//             cout<<t<<" ";
//         cout<<endl;


//         if (target == 0)
//         {
//             res.push_back(vector<int>(temp));
//             temp.pop_back(); //forgot
//             return; //since nodes >=1 , thus can prune the left
//         }
//         combinationSum2(candidates, target, i+1, temp, res);
//         temp.pop_back();

//     }

//     vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
//         vector<int> temp; vector<vector<int>> res;
//         combinationSum2(candidates, target, 0, temp, res);
//         return res;
        
//     }
// };
14055